Metro XAML - 颜色绑定不起作用

时间:2012-05-29 19:13:06

标签: c# silverlight xaml windows-runtime winrt-xaml

我一直在使用C#在Metro / XAML中编写一个复杂的自定义控件。

为了说明我的观点,我为你的考虑创造了一个最小的场景。

下面是一个简单的自定义控件:

public sealed class TestControl : Control
{
    public static DependencyProperty TestTextProperty = DependencyProperty.Register("TestText", typeof(string), typeof(TestControl), new PropertyMetadata(string.Empty));

    public string TestText
    {
        get { return (string) GetValue(TestTextProperty); }
        set { SetValue(TestTextProperty, value); }
    }

    public static DependencyProperty TestColorProperty = DependencyProperty.Register("TestColor", typeof(Color), typeof(TestControl), new PropertyMetadata(Colors.Blue));

    public Color TestColor
    {
        get { return (Color)GetValue(TestColorProperty); }
        set { SetValue(TestColorProperty, value); }
    }

    public TestControl()
    {
        this.DefaultStyleKey = typeof(TestControl);
    }
}

这个控件有两个依赖属性,一个Text属性和一个color属性。

这是Generic.xaml中的控件标准样式

<Style TargetType="local:TestControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TestControl">
                <Border
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBlock Text="{TemplateBinding TestText}">
                        <TextBlock.Foreground>
                            <SolidColorBrush Color="{Binding Source={RelativeSource Mode=TemplatedParent}, Path=TestColor}" />
                        </TextBlock.Foreground>
                    </TextBlock>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

有两个问题:

1)此代码不会编译:

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
    <local:TestControl x:Name="testcont" TestText="Hello" TestColor="Red"   />
</Grid>

错误是:

值类型XAML中的属性TestColor上不允许使用ColorPage.xaml

所以似乎Color的类型转换器被破坏了(我假设)。 为了解决这个问题,我在代码隐藏中设置了TestColor属性:

public BlankPage()
{
    this.InitializeComponent();
    testcont.TestColor = Colors.Red;
}

这允许代码编译,但颜色永远不会在模板中正确设置。我甚至使用了ValueConverter

<TextBlock.Foreground>
    <SolidColorBrush Color="{Binding Source={RelativeSource Mode=TemplatedParent}, Path=TestColor, Converter={StaticResource DebugConverter}}" />
</TextBlock.Foreground>

ValueConverter中的断点永远不会被击中,这意味着绑定会以某种方式无声地失败。

似乎没有解决方案。有人能解释一下这个问题吗?

由于

3 个答案:

答案 0 :(得分:1)

值类型依赖项属性存在已知问题:Value Type Duration is not allowed on property Duration in XAML MS表示它将被修复。目前,您可以将属性类型从“颜色”更改为“对象”。

答案 1 :(得分:0)

您需要使用Brush,而不是Color

testcont.TestColor = Brushes.Red;

DependencyProperty也应该是Brush,而不是Color。您可以使用颜色,但您必须将颜色转换为画笔。

答案 2 :(得分:0)

有两个名称空间定义颜色类型,System.DrawingSystem.Windows.Media。您的color属性可能是绘图命名空间,XAML使用媒体命名空间。

使用System.Windows.Media.Brush类型而不是Color(同样名称空间很重要),并为CLR属性添加Bindable(true)属性。

要演示,请查看Control上的背景属性声明:

[Bindable(true), Category("Appearance")]
public Brush Background
{
    get
{
    return (Brush)base.GetValue(Control.BackgroundProperty);
}
set
{
    base.SetValue(Control.BackgroundProperty, value);
}
}