2011-06-28 10 views
2

この質問はコードとXAMLを全く扱っていません。c#クラスプロパティを複合構造体にバインドする

は、だから私はこのクラスを持って、Locationと呼ばれる:

public class Location 
{ 
    public int id { get; set; } 
    public double latitude { get; set; } 
    public double longitude { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
    public bool isAnOption { get; set; } 

    public Location(int newId, double newLatitude, double newLongitude, string newName, string newType) 
    { 
     id = newId; 
     latitude = newLatitude; 
     longitude = newLongitude; 
     name = newName; 
     type = newType; 
     isAnOption = true; 
    } 

    public System.Windows.Shapes.Ellipse createIcon() 
    { 
     System.Windows.Shapes.Ellipse icon = new System.Windows.Shapes.Ellipse(); 
     SolidColorBrush brush; 
     if (isAnOption) 
     { 
      brush = new SolidColorBrush(Colors.Blue); 
     } 
     else 
     { 
      brush = new SolidColorBrush(Colors.Red); 
     } 
     brush.Opacity = 0.5; 
     icon.Fill = brush; 
     icon.Height = icon.Width = 44; 
     icon.HorizontalAlignment = HorizontalAlignment.Left; 
     icon.VerticalAlignment = VerticalAlignment.Top; 

     Thickness locationIconMarginThickness = new Thickness(0, 0, 0, 0); 
     locationIconMarginThickness.Left = (longitude - 34.672852)/(35.046387 - 34.672852) * (8704) - 22; 
     locationIconMarginThickness.Top = (32.045333 - latitude)/(32.045333 - 31.858897) * (5120) - 22; 
     icon.Margin = locationIconMarginThickness; 

     Label labelName = new Label(); 
     labelName.Content = name; 

     StackPanel locationData = new StackPanel(); 
     locationData.Children.Add(labelName); 

     ToolTip toolTip = new ToolTip(); 
     toolTip.Content = locationData; 

     icon.ToolTip = toolTip; 

     return icon; 
    } 
} 

かなりまっすぐ進みます。 createIconメソッドに注目してください。

MainWindow(WPFプロジェクト)では、List<Location> locationsと宣言し、データを入力します。私はそうのような既存のGridScrollerの「アイコン」を入れて、いくつかの点で

を:

gridScroller.Children.Add(location.createIcon()); 

、私が持っている問題は、私はのブラシの色にLocationのプロパティisAnOptionをバインドしたいです対応するアイコンのブラシの色です。つまり、Locationから派生した特定のオブジェクトのプロパティisAnOptionが変更された場合、その変更をGridScroller上にある楕円の色に反映させたいと思います。 お願いします。

おかげ

答えて

2

まず、あなたの場所のクラスがimplement INotifyPropertyChangedに必要になり、それが変更されたときにソースとしてisAnOptionを使用するすべてのバインディングが通知されます。

次にあなたがそうのようなあなたの財産にFillプロパティをバインドすることができます。

Binding binding = new Binding("isAnOption") { 
    Source = this, 
    Converter = new MyConverter(), 
}; 
icon.SetBinding(Ellipse.FillProperty, binding); 

を最後に、MyConverterは、渡されたbool値に基づいて、青や赤のブラシを返すカスタムIValueConverterだろう。

+0

うわー! CodeNaked、それは本当に速いです。 – zazkapulsk

+0

私のコードで 'INotifyPropertyChanged'と' IValueConverter'の実装について私に示唆を与えるほど親切ですか? – zazkapulsk

+0

@zazkapulsk - 私が提供したリンクには、あなたのケースに適応できるコード例があります。 – CodeNaked