代码如何在无法访问用户控件的模型中查找资源 [英] How can code find resource in model that does not have access to user control

查看:123
本文介绍了代码如何在无法访问用户控件的模型中查找资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有MergedDictionariesUserControl:

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

在我的模型中

创建了Menu.我想访问在MenuItemTemplateResources.xaml中声明的ControlTemplate. ControlTemplate看起来像这样

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>

模型引用了应用程序的主" wpf窗口(该应用程序是Winform和WPF的混合).

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

Window.FindResource找不到任何一种方法:

Window.FindResource does not find either way:

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

有什么想法吗?谢谢.

推荐答案

更复杂的方法是将资源设置为ViewModel属性的StaticResource.如果将ViewModel实例化为资源(例如

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}"/>

(前提是您在ViewModel之前声明了资源).

(provided you declare the resource before the ViewModel).

如果这不可能,则可以使用诸如Freezable的帮助程序组件,该组件绑定到ViewModel,将资源作为上述属性,并在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.

此解决方案适用于需要保留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.

根据应用程序的需求,您还可以考虑将资源设置为控件的CommandParameter,并在需要时以这种方式将其传递给ViewModel.有时我会通过文件对话框来做到这一点,例如

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>

如果OpenCommand是ViewModel提供的ICommand实现,则打开的对话框将作为参数"参数传递给ICommand的Execute和CanExecute方法.可以使用实现ICommandSource的任何控件来代替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.

这篇关于代码如何在无法访问用户控件的模型中查找资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆