Silverlight:绑定复杂属性

时间:2013-03-11 09:59:08

标签: c# wpf silverlight

首先,我将尝试解释我在做什么。我想画一个国际象棋棋盘。我有一个用户控件的单元格

<Grid x:Name="LayoutRoot">
        <Border BorderThickness="0" Margin="0" Background="{Binding CellColor, ElementName=userControl, Mode=TwoWay}"/>
        <Border x:Name="ValidMoveMarker" BorderThickness="0" Margin="0" Background="#FFC1CAB4" Opacity="0"/>
        <Image x:Name="img" Source="{Binding source, ElementName=userControl, Mode=TwoWay}" Cursor="Hand"/>

在这个CellControl的代码后面我有2个dpProperties

    public eColor? PieceColor
    {
        get { return (eColor?)GetValue(PieceColorProperty); }
        set { SetValue(PieceColorProperty, value);}
    }
    public static readonly DependencyProperty PieceColorProperty = DependencyProperty.Register("PieceColor", typeof(eColor?), typeof(CellControl), null);


    public eType? PieceType
    {
        get { return (eType?)GetValue(PieceTypeProperty); }
        set { SetValue(PieceTypeProperty, value);}
    }
    public static readonly DependencyProperty PieceTypeProperty = DependencyProperty.Register("PieceType", typeof(eType?), typeof(CellControl), null);

其中eColor和eType是枚举数。在这里我也有一个属性

public ImageSource source
        {
            get
            {
                if (PieceColor == eColor.White)
                {
                    switch (PieceType)
                    {
                        case eType.Pawn:
                            return new BitmapImage(new Uri("/PO.PC;component/Images/chess_piece_white_pawn_T.png", UriKind.Relative));
                        case eType.Knight:
                            return new BitmapImage(new Uri("/PO.PC;component/Images/chess_piece_white_knight_T.png", UriKind.Relative));
                                    ...
                        default:
                            return null;
                    }
                }
                else
                {
                    switch (PieceType)
                    {
                        case eType.Pawn:
                    }
                }
            }

现在问题是当我尝试使用像这样的控件时

<PP_Controls:CellControl PieceType="{Binding type, Mode=TwoWay}"  PieceColor="{Binding color, Mode=TwoWay}"

其中

    private eColor? _color;
    public eColor? color
    {
        get { return _color; }
        set
        {
            _color = value;
            OnPropertyChanged("color");
        }
    }

    private eType? _type;
    public eType? type
    {
        get { return _type; }
        set
        {
            _type = value;
            OnPropertyChanged("type");
        }
    }
没有发生的事情。但如果我像这样使用控制

<PP_Controls:CellControl PieceType="Bishop"  PieceColor="Black"

它完美无缺。我错过了绑定中的内容吗?这是因为“source”属性本身不是依赖属性吗?我该如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

您的目标属性是依赖项属性,源属性是实现INotifyPropertyChanged的CLR属性,因此绑定{Binding type}等应该可用 - 假设 {{1}你的“使用绑定”是具有颜色/类型属性的类型。在调试器下运行应用程序时,您应该能够通过查看“输出”窗口来判断这些绑定是否失败(在Silverlight 5中,您还可以使用绑定断点,否则您可以应用一个简单的DataContext来用于设置调试断点的绑定。)

但是,您的控件的ValueConverter属性以“懒惰”方式依赖于其他两个属性。您绑定到source属性,但当属性的计算值更改时,不会导致此绑定更新。您应该将依赖项属性更改的处理程序添加到sourcePieceColor,调用PieceType(或等效地将其转换为DP或通知属性,并显式重新计算该值)。

相关问题