2013-12-09 44 views
0

我有一個六邊形網格,我想填充一些模型從標籤繼承(歡迎使用更好的類的建議,而不是標籤)。我想從我的模型綁定一個私有變量(例如xPos)到它的網格屬性。網格綁定不與CLR屬性

我有這兩個模型。

單位

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

     } 

    } 
} 

而這種模式(從單位繼承)

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

我如何可以將綁定在士兵Grid.Row其yPos?

回答

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}}" /> 
+0

我沒有嘗試RelativeSource。謝謝 –