2011-04-01 121 views
1

我試圖寫矩形的網格,它確實會改變其對象的顏色。WPF矩形顏色綁定

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     for (int i = 0; i < size; i++) 
     { 
      main_grid.ColumnDefinitions.Add(new ColumnDefinition()); 
      main_grid.RowDefinitions.Add(new RowDefinition()); 
     } 
     for (int i = 0; i < size; i++) 
     { 
      for (int j = 0; j < size; j++) 
      { 
       cells[i, j] = new Cell { state = false, col = false }; 
       Rectangle rect = new Rectangle(); 
       Grid.SetColumn(rect, j); 
       Grid.SetRow(rect, i); 
       rect.Fill = Brushes.Orange; 
       rect.DataContext = cells[i, j]; 
       rect.SetBinding(OpacityProperty, "ev_opacity"); 
       Binding binding = new Binding("ev_col"); 
       binding.Converter = new BooleanToBrushConverter(); 
       rect.SetBinding(Rectangle.FillProperty, binding); 
       main_grid.Children.Add(rect); 
      } 
     } 
     setupTimer(); 
    } 

如何根據列設置矩形的顏色? (f.e:真 - 黑,假 - 白)

Cell類:

class Cell : INotifyPropertyChanged 
    { 
     private bool _state; 
     private bool _Col; 
     public event PropertyChangedEventHandler PropertyChanged; 
     public event PropertyChangedEventHandler PropertyChanged2; 
     public bool Col; //to set color 
     { 
      get 
      { 
       return _Col; 
      } 
      set 
      { 
       _Col = value; 
       if (PropertyChanged2 != null) 
       { 
        PropertyChanged2(this, new PropertyChangedEventArgs("event2")); 
       }; 
      } 
     } 
     public bool state //to set opacity 
     { 
      get 
      { 
       return _state; 
      } 
      set 
      { 
       _state = value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs("ev_opacity")); 
       }; 
      } 
     } 
     public static implicit operator int(Komorka c) 
     { 
      return Convert.ToInt32(c.state); 
     } 
    } 

編輯: 此代碼不能正常工作 - 後運行什麼,如果我對電網點擊發生。

回答

0

綁定:

my_rect.SetBinding(Rectangle.OpacityProperty, "state_opacity"); 
my_rect.SetBinding(Rectangle.FillProperty, 
        new Binding() 
        { 
         Converter = new BooleanToBrushConverter(), 
         Path = new PropertyPath("state_color") 
        } 
       ); 

Cell類。改變矩形的顏色取決於「state_color」變量。

class Komorka : INotifyPropertyChanged 
    { 
     private bool _state_opacity; 
     private bool _state_color; 

     public event PropertyChangedEventHandler PropertyChanged; 

     public bool state_color 
     { 
      get { return _state_color; } 
      set 
      { 
       _state_color = value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs("state_color")); 
       }; 
      } 
     } 

     public bool state_opacity 
     { 
      get 
      { 
       return _state_opacity; 
      } 
      set 
      { 
       _state_opacity = value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs("state_opacity")); 
       }; 
      } 
     } 
    } 
} 

Converter類:

class BoolToBrush : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value != null) 
      { 
       if ((bool)value == false) 
       { 
        return new SolidColorBrush(Colors.Orange); 
       } 
       else 
       { 
        return new SolidColorBrush(Colors.Black); 
       } 
      } 
      else 
      { 
       return new SolidColorBrush(Colors.Red); 
      } 
     } 
+0

在這個答案中不太清楚你是如何解決點擊事件的。 – 2011-04-03 12:32:03

3

常用的方法是在綁定上使用ValueConverter

只需製作一個將布爾轉換爲Brush的ValueConverter即可。

在WPF中,如果您有CellControl,您也可以在Cell的模板中使用DataTrigger

EDIT

要添加代碼的結合:

Binding binding = new Binding("State"); 
    binding.Converter = new BooleanToBrushConverter(); 
    _rectangle.SetBinding(Rectangle.FillProperty, binding); 
+0

行,我明白,但我怎麼能結合矩形和我在CS代碼BOOL如果我arledy有轉換器?我沒有在xaml文件中的矩形,所以我不能這樣做。 – mkd 2011-04-01 14:40:42

+0

我更新了我的答案。 – 2011-04-01 15:27:55

+0

我更新了我的問題。 – mkd 2011-04-01 16:23:51