0

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

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

它运行良好。我的绑定中缺少什么吗?这是因为“源”属性本身不是依赖属性吗?如何解决我的问题?

4

1 回答 1

0

Your target properties are dependency properties, and the source properties are CLR properties implementing INotifyPropertyChanged, so your binding {Binding type} etc should work - assuming that the DataContext to your "use with binding" is the type with the color/type properties. You should be able to tell if these bindings are failing by looking at the Output window when you run your application under the debugger (in Silverlight 5 you could also use a binding breakpoint, or otherwise you can apply a trivial ValueConverter to the binding to set a breakpoint for debugging).

However your control's source property depends on two other properties in a 'lazy' fashion. You are binding to the source property, but nothing will cause this binding to update when the property's computed value changes. You should add a dependency property changed handler to PieceColor and PieceType which calls OnPropertyChanged("source") (or equivalently convert it to a DP or notifying property, and explicitly recompute the value).

于 2013-03-11T12:44:39.560 回答