[英]Dependency property is not working for me on a WPF UserControl
I created my own user control with dependency properties and added it to my main window, where I now want to be able set the dependency properties. 我创建了具有依赖项属性的自己的用户控件,并将其添加到我的主窗口中,现在我希望可以在其中设置依赖项属性。 The properties are not taking the value I set in the XAML of the main window and I am not sure what I am missing. 这些属性未采用我在主窗口的XAML中设置的值,并且不确定所缺少的内容。 In the code I set the default value for the FillBrush property to Yellow. 在代码中,我将FillBrush属性的默认值设置为Yellow。 In the XAML I set it to Red. 在XAML中,我将其设置为Red。 When I click the Test button, it shows the property to be Yellow. 当我单击测试按钮时,它显示为黄色。 Here is the code: 这是代码:
Window XAML 视窗XAML
<Window x:Class="Test.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:Test"
Title="Window2" Height="200" Width="600">
<StackPanel>
<test:TestUserControl x:Name="myControl"
FillBrush="Red" VerticalAlignment="Center" Margin="20"/>
<Button Height="24" Width="300" Content="Test" Click="Button_Click" />
<TextBox x:Name="debugTextBox" Margin="20"/>
</StackPanel>
</Window>
Window Code Behind 背后的窗口代码
public partial class Window2 : Window
{
public static readonly DependencyProperty FillBrushProperty =
DependencyProperty.Register("FillBrush", typeof(Brush), typeof(Window2),
new UIPropertyMetadata(Brushes.Yellow));
public Brush FillBrush
{
get { return (Brush)GetValue(FillBrushProperty); }
set { SetValue(FillBrushProperty, value); }
}
public Window2() { InitializeComponent(); }
private void Button_Click(object sender, RoutedEventArgs e)
{
this.debugTextBox.Text =
"Red: " + Brushes.Red +
" Yellow: " + Brushes.Yellow +
" Actual: " + this.FillBrush;
}
}
User Control XAML 用户控件XAML
<UserControl x:Class="Test.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TextBlock Text="User Control"/>
</UserControl>
User Control Code Behind 背后的用户控制代码
public partial class TestUserControl : UserControl
{
public static readonly DependencyProperty FillBrushProperty =
DependencyProperty.Register("FillBrush", typeof(Brush), typeof(TestUserControl),
new UIPropertyMetadata(Brushes.Cyan));
public Brush FillBrush
{
get { return (Brush)GetValue(FillBrushProperty); }
set { SetValue(FillBrushProperty, value); }
}
public TestUserControl()
{
InitializeComponent();
}
}
What am I missing? 我想念什么?
In the XAML you are setting the Fillbrush
value on TestUserControl
to Red, but when the button is clicked it shows the Fillbrush
value for Window2
, not TestUserControl
. 在XAML中,您将TestUserControl
上的Fillbrush
值设置为Red,但是单击该按钮时,它将显示Window2
而不是TestUserControl
的Fillbrush
值。 And since the Fillbrush
value for Window2
is not set in XAML it still has the default value of Yellow. 而且由于XAML中未设置Window2
的Fillbrush
值,因此它仍具有默认值Yellow。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.