How can code find resource in model that does not have access to user control

空扰寡人 提交于 2019-12-25 02:50:11

问题


I have a UserControl that has MergedDictionaries:

<UserControl.Resources>
  <ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../LimitResources.xaml" />
        <ResourceDictionary Source="../MenuItemTemplateResources.xaml" />
     </ResourceDictionary.MergedDictionaries>
   ...
</UserControl.Resources>

in my Model a Menu is created. I want to access ControlTemplate declared in MenuItemTemplateResources.xaml. The ControlTemplate looks like this

<ControlTemplate x:Key="SubMenuItemCombo" TargetType="MenuItem">
.....
</ControlTemplate>

Model has reference to "main" wpf window of the application (The application is hybrid of Winform and WPF).

Window.FindResource does not find either way:

FindResource("SubMenuItemCombo");
FindResource(new ComponentResourceKey(typeof(MenuItem), "SubMenuItemCombo"));

Any ideas? Thank you.


回答1:


A more sophisticated approach would be to set the resource as a StaticResource to a property of your ViewModel. You can do this in XAML if you instantiate your ViewModel as a resource, like

<my:ViewModel x:Key="viewModel" myResource="{StaticResource myResource}"/>

(provided you declare the resource before the ViewModel).

If this is not possible, you can use a helper component like a Freezable which binds to the ViewModel, takes the resource as a property as above, and sets the required property on the ViewModel.

This solution is for cases where you want to keep the ViewModel without code dependency on the resource, e.g. in a control library where the resource is not known beforehand.

Depending on what your application needs, you might also consider setting the resource to a control's CommandParameter and passing it to the ViewModel this way when needed; I sometimes do this with file dialogs, like

<Button x:Name="OpenButton" Command="{Binding OpenCommand}" CommandParameter="{StaticResource openDialog}">Open File...</Button>

If OpenCommand is an ICommand implementation provided by the ViewModel, then the open dialog would be passed as the "parameter" argument to the ICommand's Execute and CanExecute methods. Instead of a Button, one could use any control which implements ICommandSource.




回答2:


The simplest solution is to define a property in your resource class's code behind file

public string StrResource1
{
   get {return FindResource("SubMenuItemCombo");}
}

then you can access that outside the resource class. You may also consider to declare a static propertyif you do not want to access it via an instance.



来源:https://stackoverflow.com/questions/15946337/how-can-code-find-resource-in-model-that-does-not-have-access-to-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!