简体   繁体   English

具有依赖项属性WPF的用户控件DataContext /绑定问题

[英]User Control DataContext/Binding Issue with Dependency Property WPF

Ok, so my problem is I have a user control. 好的,所以我的问题是我有一个用户控件。 In the xaml I bind some colors to color properties that I have created as shown below. 在xaml中,我将一些颜色绑定到我创建的颜色属性中,如下所示。

<GradientStop x:Name="stop1" Color="{Binding Color1}" Offset="0"/>
<GradientStop x:Name="stop2" Color="{Binding Color2}" Offset="1"/>

In my code behind I have a DependencyProperty that I have declared as shown below. 在后面的代码中,我声明了一个DependencyProperty,如下所示。

public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool), typeof(Bin), 
new PropertyMetadata(new PropertyChangedCallback(Bin.IsActivePropertyChanged)));

The dependency property has a PropertyChangedCallback that it calls called IsActivePropertyChanged as shown below. 依赖项属性具有一个称为IsActivePropertyChanged的PropertyChangedCallback,如下所示。

private static void IsActivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
            Bin b = (Bin)d;
            if((bool)e.NewValue)

            {
                b.Color1 = Color.FromArgb(0xFF, 0x3E, 0x3E, 0x3E);
                b.Color2 = Colors.Red;
                b.Color3 = Colors.Red;
                b.Color4 = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
            }
            else
            {
                b.Color1 = Color.FromArgb(0xFF, 0x3E, 0x3E, 0x3E);
                b.Color2 = Color.FromArgb(0xFF, 0x83, 0x83, 0x83);
                b.Color3 = Color.FromArgb(0xFF, 0x63, 0x63, 0x63);
                b.Color4 = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
            }
        }

If I use the constructor below, the color changes inside of the constructor work fine, however, my IsActivePropertyChangedEvent never gets fired. 如果我在下面使用构造函数,则构造函数内部的颜色变化可以正常工作,但是,我的IsActivePropertyChangedEvent永远不会触发。 I am assuming because of the DataContext assignment in the constructor. 我假设由于构造函数中的DataContext分配。

 public Bin()
        {
            Color1 = Color.FromArgb(0xFF, 0x3E, 0x3E, 0x3E);
            Color2 = Color.FromArgb(0xFF, 0x83, 0x83, 0x83);
            Color3 = Color.FromArgb(0xFF, 0x63, 0x63, 0x63);
            Color4 = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
            InitializeComponent();
            DataContext = this;
        }

If I comment out the DataContext assignment and use the constructor below, my Color assignments do not work, but the IsActivePropertyChanged event fires fine. 如果我注释掉DataContext分配并使用下面的构造函数,则我的Color分配不起作用,但是IsActivePropertyChanged事件正常运行。

public Bin()
            {
                Color1 = Color.FromArgb(0xFF, 0x3E, 0x3E, 0x3E);
                Color2 = Color.FromArgb(0xFF, 0x83, 0x83, 0x83);
                Color3 = Color.FromArgb(0xFF, 0x63, 0x63, 0x63);
                Color4 = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
                InitializeComponent();
                //DataContext = this;
            }

My question is how can i get the binding to work correctly and have my event fire as well. 我的问题是我如何才能使绑定正常工作并触发事件。 I have tried setting the DataContext="{Binding RelativeSource={RelativeSource Self}}" (instead of setting the DataContext in the code behind) of the items that are bound to the Color Properties in XAML, a rectangle and a polygon, but that didn't seem to work. 我尝试设置绑定到XAML的颜色属性,矩形和多边形的项目的DataContext="{Binding RelativeSource={RelativeSource Self}}" (而不是在后面的代码中设置DataContext),但是似乎没有用。 Thanks in advance for any help. 在此先感谢您的帮助。

When writing your own control, you shouldn't mess with the DataContext of the control itself. 在编写自己的控件时,您不应DataContext控件本身的DataContext

Instead, on the binding of the GradientStop , you can use RelativeSource={RelativeSource AncestorType=Bin} (assuming Bin is your control). 相反,在GradientStop的绑定上,可以使用RelativeSource={RelativeSource AncestorType=Bin} (假设Bin是您的控件)。 Or you can define a template and use TemplateBinding. 或者,您可以定义模板并使用TemplateBinding。 Check this answer I wrote a while back for a similar question - it has more detailed description of how this works. 检查这个答案,我前不久写了一个类似的问题-它有更详细的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM