generic.Xaml 内的依赖项 属性 的模板绑定

Template binding of dependency property inside generic.Xaml

我有一个自定义单选按钮控件并在其中定义了一个依赖项 属性。在 generic.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>

并在主 page.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>