<Window x:Class="WpfApplication16.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dc="clr-namespace:WpfApplication16"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="TestStyle">
<Setter Property="dc:T1.IntTest" Value="5"/>
<Setter Property="dc:T2.IntTest" Value="10"/>
</Style>
</Window.Resources>
<StackPanel>
<StackPanel Background="green" Margin="0,0,0,10">
<dc:T1 x:Name="t1" IntTest="5" Text="T1 = 5" Background="red"/>
<dc:T2 x:Name="t2" IntTest="10" Text="T2 = 10" Background="red"/>
<TextBlock Text="{Binding ElementName=t1, Path=IntTest}"></TextBlock>
<TextBlock Text="{Binding ElementName=t2, Path=IntTest}"></TextBlock>
</StackPanel>
<StackPanel Background="Yellow">
<dc:T1 x:Name="tt1" Style="{StaticResource TestStyle}" Background="red" Text="T1 = TestStyle"/>
<dc:T2 x:Name="tt2" Style="{StaticResource TestStyle}" Background="red" Text="T2 = TestStyle"/>
<TextBlock Text="{Binding ElementName=tt1, Path=IntTest}"></TextBlock>
<TextBlock Text="{Binding ElementName=tt2, Path=IntTest}"></TextBlock>
</StackPanel>
</StackPanel>
</Window>
Shared Dependency Property in WPF
Код:
Код:
namespace WpfApplication16
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class T1 : TextBlock
{
// Definition of Dependency property
public static readonly DependencyProperty IntTestProperty = DependencyProperty.Register("IntTest", typeof(int), typeof(T1));
public int IntTest
{
get { return (int)GetValue(IntTestProperty); }
set { SetValue(IntTestProperty, value); }
}
}
// definition of class which uses the dependency property defined above
public class T2 : TextBlock
{
public static readonly DependencyProperty IntTestProperty = T1.IntTestProperty.AddOwner(typeof(T2));
public int IntTest
{
get { return (int)GetValue(IntTestProperty); }
set { SetValue(IntTestProperty, value); }
}
}
}
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class T1 : TextBlock
{
// Definition of Dependency property
public static readonly DependencyProperty IntTestProperty = DependencyProperty.Register("IntTest", typeof(int), typeof(T1));
public int IntTest
{
get { return (int)GetValue(IntTestProperty); }
set { SetValue(IntTestProperty, value); }
}
}
// definition of class which uses the dependency property defined above
public class T2 : TextBlock
{
public static readonly DependencyProperty IntTestProperty = T1.IntTestProperty.AddOwner(typeof(T2));
public int IntTest
{
get { return (int)GetValue(IntTestProperty); }
set { SetValue(IntTestProperty, value); }
}
}
}