【发布时间】:2014-04-08 20:13:58
【问题描述】:
我想将 Style 公开为依赖属性。基本上中间有一个矩形指示器,使用控件将公开一个样式依赖属性,用于包含要设置的控件。它将允许那些包含控件的人根据他们自己对项目的了解来提供一种着色样式。
<ItemsControl ItemsSource="{Binding Rows}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RowName}"/>
<ItemsControl ItemsSource="{Binding Statuses}">
<Rectangle Style="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Path=RectangleStyle}"/>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
自定义控件代码隐藏中的依赖属性。
public Style RectangleStyle
{
get { return (Style)GetValue(RectangleStyleProperty); }
set { SetValue(RectangleStyleProperty, value); }
}
public static readonly DependencyProperty RectangleStyleProperty =
DependencyProperty.Register("RectangleStyle", typeof(Style), typeof(MyControl), new PropertyMetadata(null));
然后会像这样使用:
<MyControl>
<MyControl.RectangleStyle>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Fill" Value="Red"/>
</Style>
</MyControl.RectangleStyle>
</MyControl>
这根本不起作用,我不确定我的方法是否正确。
【问题讨论】:
标签: wpf wpf-controls dependency-properties