如何将完整的类绑定到依赖项属性?

时间:2016-08-09 13:26:05

标签: c# wpf class binding

我有一个自定义类,我想绑定到依赖项属性。这样它就会在更改时更新UI。然而,它不能按我编程的方式工作。谁能告诉我我做错了什么?

以下是一些代码段:

UserControl XAML:

    breed [stemcells stemcell]

    globals [
      r
      lambda
      probability
      ]


    to setup
      clear-all
      set r 0.5544
      set lambda 1.233
      set probability .85
      create-stemcells 1
      end

    to mitosis 
      ask stemcells [
        if random-float (2 * r * lambda) < 2 * r * lambda [   ;; this is superfluous, always true
          ifelse random-float 1. < probability [
         hatch 1 [
          let free-neighbor one-of neighbors with [not any? turtles-here]
          ifelse free-neighbor != nobody [
            move-to free-neighbor
            ]
          [; this die is executed if there is no free space in the neighborhood
            output-show " Dead because of no free space in neighborhood"
            die
            ]
          ]
        ;set breed stemcells        ;; not necessary in an ask of stemcells
        ] 
          [;this die is executed if probablity compares to random-float and the hatch is not done
        output-show " Dead because did not hatch"
        die
        ]
          ] 
        ]
      e

UserControl C#

<UserControl x:Class="HexButton.HexButtonControl"
         .../>
<Canvas x:Name="LayoutRoot" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown">
    <Polygon .../>
    <Rectangle
             Name="R_Unit"
             Height="70"
             Width="48"                 
             Canvas.Left="28"
             Canvas.Top="10"
     />
    <Label Name="L_UnitCount"
        ...
        Foreground="{Binding Path=LabelColor}"
        Canvas.Left="80" 
        Canvas.Top="31"/>        
</Canvas>
</UserControl>

模特:

    {
    //....
    #region Move

    /// <summary>
    /// Gets or sets if the unit can move on this hexagon
    /// </summary>
    public bool Move
    {
        get { return (bool)GetValue(MoveProperty); }
        set
        {
            SetValue(MoveProperty, value);
            MoveChanged();
        }
    }

    /// <summary>
    /// Identified the move dependency property
    /// </summary>
    public static readonly DependencyProperty MoveProperty =
        DependencyProperty.Register("Move", typeof(bool),
          typeof(HexButtonControl), new PropertyMetadata(false));

    #endregion

    #region Unit

    /// <summary>
    /// Gets or sets the LabelText which is displayed next to the (unit-)rectangle
    /// </summary>
    public Unit AssinedUnit
    {
        get { return (Unit)GetValue(AssignedUnitProperty); }
        set
        {
            SetValue(AssignedUnitProperty, value);
            UnitChanged();
        }
    }

    /// <summary>
    /// Identified the LabelText dependency property
    /// </summary>
    public static readonly DependencyProperty AssignedUnitProperty =
        DependencyProperty.Register("AssignedUnit", typeof(Unit),
          typeof(HexButtonControl), new PropertyMetadata(new Unit()));

    #endregion   

    #endregion

    public HexButtonControl()
    {
        InitializeComponent();
        LayoutRoot.DataContext = this;
        Hexagon.Stroke = new SolidColorBrush(Colors.ForestGreen);
        //UnitChanged();
    }

private void MoveChanged()
    {
        if (Move)
        {
            OldBorderBrush = Hexagon.Stroke;
            Hexagon.Stroke = new SolidColorBrush(Colors.Yellow);
        }
        else
        {
            Hexagon.Stroke = OldBorderBrush;
        }
    }

    private void UnitChanged()
    {
        string unitPic = @"C:\Users\Public\Pictures\Sample Pictures\Test\";            
        L_UnitCount.Content = AssinedUnit.UnitCount.ToString();

        switch (AssinedUnit.UnitKind)
        {
            case UnitType.Ranger: unitPic+="A-";
                break;
            case UnitType.Meele: unitPic+="F-";
                break;
            case UnitType.Cavalry: unitPic+="R-";
                break;
            default: break;
        }

        switch (AssinedUnit.Strength)
        {
            case UnitStrength.Light: unitPic += "light.png";
                break;
            case UnitStrength.Medium: unitPic += "medium.png";
                break;
            case UnitStrength.Heavy: unitPic += "heavy.png";
                break;
            default: break;
        }

        R_Unit.Fill = new SolidColorBrush(Colors.Black);
        R_Unit.Fill = new ImageBrush(new BitmapImage(new Uri(unitPic)));

    }
//....
}

UnitClass:

 public class HexModelObject
{
    private Unit _assignedUnit;
    public Unit AssignedUnit
    {
        get { return _assignedUnit; }
        set
        {
            _assignedUnit = value;
            OnPropertyChanged("Unit");
        }
    }
//...
        public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

主窗口:

public class Unit
{
    public UnitType UnitKind { get; set; }
    public UnitStrength Strength { get;  set; }
    public int UnitCount { get;  set; }

    public Unit() : this(UnitType.Ranger, UnitStrength.Light, 3) { }

    public Unit(UnitType type, UnitStrength strengt, int unitCount)
    {
        UnitKind = type;
        Strength = strengt;
        UnitCount = unitCount;
    }

}

public enum UnitType
{
    Meele,
    Ranger,
    Cavalry
}

public enum UnitStrength
{
    Light,
    Medium,
    Heavy
}

当我添加UnitChanged();它工作一次的UserCotrol的构造函数,所以没有丢失图像文件等问题。 当我不添加它时,矩形和标签是空白的。

我做错了什么?它适用于我添加的所有其他依赖属性,但它们都是基本类型(如int,string,Brush等)。

0 个答案:

没有答案