泛型中依赖项属性的模板绑定.萨姆尔
本文关键字:绑定 依赖 属性 泛型 | 更新日期: 2023-09-27 18:31:02
我有一个自定义单选按钮控件,并在其中定义了一个依赖项属性。在通用中。Xaml,我放了一个椭圆并将 Fill 属性设置为 TemplateBinding SelectedColor。在这里,SelectColor 是一个依赖属性,我在使用此控件的主页中分配了它的值。但是椭圆的这个填充属性不起作用。
public sealed class CustomControl1 : RadioButton
{
public CustomControl1()
{
this.DefaultStyleKey = typeof(CustomControl1);
}
public Color SelectedColor
{
get { return (Color)GetValue(SelectedColorProperty); }
set { SetValue(SelectedColorProperty, value); }
}
public static readonly DependencyProperty SelectedColorProperty =
DependencyProperty.Register("SelectedColor", typeof(Color), typeof(CustomControl1), new PropertyMetadata(Colors.Red));
}
并将 generic.xaml 中的单选按钮样式设置为
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App20">
<Style TargetType="local:CustomControl1" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomControl1">
<Grid BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Height="32" VerticalAlignment="Top">
<ContentPresenter x:Name="ContentPresente" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" TextWrapping="Wrap" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Ellipse Height="25" Width="25" Fill="{TemplateBinding SelectedColor}" />
</ContentPresenter>
</Grid>
<ContentPresenter x:Name="ContentPresenter" Foreground="Black" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" TextWrapping="Wrap" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
并在主页.xaml 中创建此自定义控件的实例
<local:CustomControl1 SelectedColor="Green" Content="qweerrrt" />
Shape.Fill DP不是颜色,而是画笔。你的DP应该是画笔。
或者你可以写:
<Ellipse Height="25" Width="25">
<Ellipse.Fill>
<SolidColorBrush Color="{TemplateBinding SelectedColor}"/>
</Ellipse.Fill>
</Ellipse>