简体   繁体   English

Silverlight:绑定复杂属性

[英]Silverlight: binding on complex property

First of all i will try to explain what i am doing. 首先,我将尝试解释我在做什么。 I am trying to draw a chess board. 我试图画一个棋盘。 I have a user controll for cell 我有一个用户控制单元

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

In code behind of this CellControl i have 2 dpProperties 在此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);

where eColor and eType are enumerators. 其中eColor和eType是枚举数。 Here I also have one property 在这里我也有一个财产

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:
                    }
                }
            }

Now problem is when i try to use the control like this 现在的问题是当我尝试使用这样的控件时

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

where 哪里

    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");
        }
    }

nothings happens. 什么也没发生。 But if i use control like this 但是如果我使用这样的控件

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

it is working perfectly. 它工作正常。 Am I missing something in my bindings? 我在绑定中缺少什么吗? Is this because "source" property is not dependency property itself? 这是因为“源”属性本身不是依赖项属性吗? How can I workaround my problem? 我该如何解决我的问题?

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. 您的目标属性是依赖项属性,源属性是实现INotifyPropertyChanged CLR属性,因此您的绑定{Binding type}等应该可以工作- 假设要与您的“结合使用”使用的DataContext是具有color / type属性的类型。 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). 当您在调试器下运行应用程序时,您应该能够通过查看“输出”窗口来判断这些绑定是否失败(在Silverlight 5中,您也可以使用绑定断点,否则您可以对绑定进行简单的ValueConverter设置)调试断点)。

However your control's source property depends on two other properties in a 'lazy' fashion. 但是,控件的source属性以“惰性”方式依赖于其他两个属性。 You are binding to the source property, but nothing will cause this binding to update when the property's computed value changes. 您正在绑定到source属性,但是当属性的计算值更改时,没有什么会导致此绑定更新。 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). 您应该将更改了依赖项属性的处理程序添加到PieceColorPieceType ,该处理程序将调用PieceType OnPropertyChanged("source") (或等效地将其转换为DP或通知属性,并明确地重新计算该值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM