网格绑定不适用于CLR属性

时间:2013-12-09 13:24:17

标签: c# wpf visual-studio-2010

我有一个六边形网格,我想填充一些从标签继承的模型(建议使用更好的类来代替标签)。我想将一个私有变量(例如xPos)从我的模型绑定到它的网格属性。

我有这两个模型。

单元

public class Unit : Label, INotifyPropertyChanged
{
    public Unit() { } //Grass
    public Unit(int x, int y)
    {
        this.xPos = x;
        this.yPos = y;
        this.mouseLeft = false;
        this.mouseRight = false;
    }

    public static readonly RoutedEvent ClickEvent;

    private int _xPos, _yPos;

    private bool mouseLeft, mouseRight;


    public int xPos
    {
        get { return _xPos; }
        set
        {
            _xPos = value;
            NotifyPropertyChanged("xPos");
        }
    }

    public int yPos
    {
        get { return _yPos; }
        set
        {
            _yPos = value;
            NotifyPropertyChanged("yPos");
        }
    }

    private string _type = "none";

    public string type
    {
        get { return _type; }
        set
        {
            _type = value;
        }

    }

    static Unit()
    {
        ClickEvent = ButtonBase.ClickEvent.AddOwner(typeof(Unit));
    }

    public event RoutedEventHandler Click
    {

        add { AddHandler(ClickEvent, value); }

        remove { RemoveHandler(ClickEvent, value); }

    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        this.mouseLeft = e.LeftButton.ToString().Equals("Pressed");
        this.mouseRight = e.RightButton.ToString().Equals("Pressed");


        base.OnMouseDown(e);

        CaptureMouse();

    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {

        base.OnMouseUp(e);


        if (this.mouseRight)
        {
            this.xPos = (int)this.GetValue(Grid.RowProperty);
            this.yPos = (int)this.GetValue(Grid.ColumnProperty);
            this.type = this.GetType().Name;

        }
        else if (this.mouseLeft)
        {
            MainWindow.objectInspector.selectedUnit = this;


        }
        else
        {
            MessageBox.Show("Kaos");
        }
        if (IsMouseCaptured)
        {

            ReleaseMouseCapture();

            if (IsMouseOver)

                RaiseEvent(new RoutedEventArgs(ClickEvent, this));

        }

    }
}

这个模型(继承自Unit)

public class Soldier : Unit
{
    public Soldier()
    {

        // get bitmapimage from resources and assign to img
        Uri resourceUri = new Uri("Resources/Soldier.jpg", UriKind.Relative);
        StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

        BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
        var brush = new ImageBrush();
        brush.ImageSource = temp;

        this.Background = brush;
    }
}

这个WPF

   <local:Soldier xPos="10" yPos="5" Grid.Row="Binding Path=this.yPos" Grid.Column="Binding Path=this.xPos" />

   <local:Soldier xPos="7" yPos="7" Grid.Row="Binding Path=this.yPos" Grid.Column="Binding Path=this.xPos" />

如何绑定Soldiers Grid.Row到它的yPos?

1 个答案:

答案 0 :(得分:1)

我无法理解为什么你为你的问题创造了这样一个误导性的标题。你试图绑定到私有变量...你试图像其他人一样绑定到公共属性。话虽如此,您是否尝试将BindingRelativeSource使用?:

<local:Soldier xPos="10" yPos="5" Grid.Row="{Binding yPos, RelativeSource=
    {RelativeSource Self}}" Grid.Column="{Binding xPos, RelativeSource=
    {RelativeSource Self}}" />    
<local:Soldier xPos="7" yPos="7" Grid.Row="{Binding yPos, RelativeSource=
    {RelativeSource Self}}" Grid.Column="{Binding xPos, RelativeSource=
    {RelativeSource Self}}" />