Я создал свой собственный пользовательский элемент управления со свойствами зависимостей и добавил его в главное окно, где я теперь хочу иметь возможность устанавливать свойства зависимостей.Свойства не принимают значение, которое я установил в XAML главного окна, и я не уверен, что мне не хватает.В коде я установил значение по умолчанию для свойства FillBrush на Желтый.В 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>
Код окна сзади
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;
}
}
ПользовательУправление 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>
Код управления пользователя позади
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();
}
}
Чего мне не хватает?