从后面的代码设置 ResourceDictionary DataContext [英] Set a ResourceDictionary DataContext from code behind

查看:59
本文介绍了从后面的代码设置 ResourceDictionary DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的资源字典背后的代码中设置一个 ResourceDictionary DataContext.

I'm trying to set a ResourceDictionary DataContext, from the code behind of my Resource Dictionary.

我有一个使用自己样式(资源字典)的数据模板,该样式包含一个具有自己样式的复选框:

I have a Data Template that uses its own style (the Resource Dictionary), the style contains a checkbox with its own style :

<Style x:Key="CheckBoxStyle" TargetType="CheckBox">
    <EventSetter Event="CheckBox.Checked" Handler="CheckBox_Checked"/>
    <EventSetter Event="CheckBox.Unchecked" Handler="CheckBox_Unchecked"/>        
</Style>

在 CheckBox_Checked 事件中,我想引用字典的父级(用户控件)视图模型来执行函数,但是因为资源字典没有从控件事件内部设置 DataContext 的 DataContext 属性,如下所示:

In the CheckBox_Checked event, I want to reference the dictionary's parent (a User Control) View Model, to execute a function, but because Resource Dictionaries do not have a DataContext property setting the DataContext from inside a control event, like this :

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {  
      MyViewModel viewModel = (MyViewModel)DataContext;
    }

不起作用(当然).

我想我需要获得祖先(资源字典用户控件)的句柄,但不知道如何做到这一点 - 或者可能有其他方法..

I think I need to get a handle to the Ancestor (the Resource Dictionary User Control), but don't know how to do this - or there may be another way..

谢谢

推荐答案

正如@dowhilefor 的评论所说,Resource Dictionaries 只是资源的集合,因此不需要 DataContext.但是,您可以将代码隐藏文件添加到 ResourceDictionary,这可能就是您要查找的内容.

As @dowhilefor's comment says, Resource Dictionaries are simply a collection of resources, so do not need a DataContext. You can, however, add a code-behind file to the ResourceDictionary, which may be what you're looking for.

在与 ResourceDictionary 相同的目录中创建一个新类,并将其命名为 ResourceDictionaryName.xaml.cs.它将成为您的 ResourceDictionary 的代码隐藏文件.

Create a new class in the same directory as your ResourceDictionary and name it ResourceDictionaryName.xaml.cs. It will become the code-behind file for your ResourceDictionary.

打开新的 .cs 文件,并确保以下内容存在(不记得是否自动添加):

Open the new .cs file, and make sure the following is there (Can't remember if it's added automatically or not):

public partial class ResourceDictionaryName
{
    public ResourceDictionaryName()
    {
        InitializeComponent();
    }
}

接下来,打开您的 XAML 文件并将以下 x:Class 属性添加到 ResourceDictionary 标签:

Next, open your XAML file and add the following x:Class attribute to the ResourceDictionary Tag:

<ResourceDictionary x:Class="MyNamespace.ResourceDictionaryName" ... />

现在您的 ResourceDictionary 实际上是一个类,并且可以有一个代码隐藏文件.

Now your ResourceDictionary is actually a class, and can have a code-behind file.

编辑

为了响应您的编辑,我将使用 CheckBox 本身并获取 CheckBox 的 DataContext,或者遍历可视化树以找到我正在寻找的 UserControl,然后获取它的 Data Context

In response to your edits, I would use the CheckBox itself and get either the CheckBox's DataContext, or traverse up the Visual Tree to find the UserControl I'm looking for and then get it's Data Context

简单的方法:

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{  
    var cbx = sender as CheckBox;
    MyViewModel viewModel = (MyViewModel)cbx.DataContext;
}

如果 CheckBox 的 DataContext 不是您要查找的 ViewModel:

If CheckBox's DataContext is not the ViewModel you're looking for:

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{  
    var cbx = sender as CheckBox;
    var userControl = FindAncestor<MyUserControl>(cbx);
    MyViewModel viewModel = (MyViewModel)myUserControl.DataContext;
}

public static T FindAncestor<T>(DependencyObject current)
    where T : DependencyObject
{
    current = VisualTreeHelper.GetParent(current);

    while (current != null)
    {
        if (current is T)
        {
            return (T)current;
        }
        current = VisualTreeHelper.GetParent(current);
    };
    return null;
}

这篇关于从后面的代码设置 ResourceDictionary DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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