1

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

并在 main page.xaml 中创建了此自定义控件的实例

 <local:CustomControl1 SelectedColor="Green" Content="qweerrrt" />
4

1 回答 1

0

Shape.Fill DP 不是颜色而是画笔。你的DP应该是Brush。

或者你可以写:

<Ellipse Height="25" Width="25">
    <Ellipse.Fill>
        <SolidColorBrush Color="{TemplateBinding SelectedColor}"/>
    </Ellipse.Fill>
</Ellipse>
于 2016-01-19T12:44:09.540 回答