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

时间:2016-01-19 12:30:58

标签: c# xaml win-universal-app uwp-xaml

我有一个自定义单选按钮控件,并在其中定义了依赖项属性。在generic.Xaml中,我放了一个椭圆并将Fill属性设置为TemplateBinding SelectedColor。这里SelectColor是一个依赖属性,我在mainpage中使用此控件分配了它的值。但椭圆的这个填充属性不起作用。

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" />

1 个答案:

答案 0 :(得分:0)

Shape.Fill DP不是彩色它是刷子。你的DP应该是刷子。

或者你可以写:

<Ellipse Height="25" Width="25">
    <Ellipse.Fill>
        <SolidColorBrush Color="{TemplateBinding SelectedColor}"/>
    </Ellipse.Fill>
</Ellipse>