2011-03-21 41 views
0

我需要根據綁定中的文本塊文本字符串值更改邊框背景顏色。我計劃使用觸發器,但在Silverlight中不受支持。我正在尋找任何有關如何在Silverlight中實現的建議。先謝謝你!如何根據Silverlight中的綁定值更改對象的顏色?

XAML:

<data:DataGridTemplateColumn Header="Y Position" Width="100"> 
       <data:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <Border Background="Red" Width="10" Height="18" VerticalAlignment="Center" Margin="0,0,10,0" /> 
          <TextBlock Text="{Binding Y}" VerticalAlignment="Center" /> 
         </StackPanel> 
        </DataTemplate> 
       </data:DataGridTemplateColumn.CellTemplate> 
      </data:DataGridTemplateColumn> 

視圖模型代碼:

public class MainPage_ViewModel : INotifyPropertyChanged 
{ 
    public 
    public MainPage_ViewModel() 
    { 
     coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 1, Y = 2 }));   
     coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 2, Y = 4 }));   
     coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 3, Y = 6 }));   
     coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 4, Y = 8 }));   
     coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 5, Y = 10 }));   
     coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 6, Y = 12 })); 
    } 

    public ObservableCollection<Coordinate_DataViewModel> Coordinates  
    {   
     get { return coordinates; }   
     set   
     {    
      if (coordinates != value)    
      {     
       coordinates = value;     
       OnPropertyChanged("Coordinates");    
      }   
     }  
    }  
    private ObservableCollection<Coordinate_DataViewModel> coordinates = new ObservableCollection<Coordinate_DataViewModel>();  
    public event PropertyChangedEventHandler PropertyChanged;  

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

    public void DeleteCoordinate(Coordinate_DataViewModel dvmToDelete)  
    {   
     coordinates.Remove(dvmToDelete);  
    } 

    public void UpdateCoordinate(Coordinate_DataViewModel dvmToDelete) 
    { 

    } 
} 

//Model 
public class Coordinate_Model 
{  
    public double X;  
    public double Y; 
} 

//DataViewModel 
public class Coordinate_DataViewModel 
{  
    public Coordinate_DataViewModel(Coordinate_Model model)  
    {   
     this.underlyingModel = model;  
    }  

    private Coordinate_Model underlyingModel;  
    public double X  
    {   
     get { return underlyingModel.X; }   
     set { underlyingModel.X = value; }  
    }  

    public double Y  
    {   
     get { return underlyingModel.Y; }   
     set { underlyingModel.Y = value; }  
    }  public string XYCoordinate  

    {   
     get { return "(" + X + "," + Y + ")"; }  
    } 
} 

回答

1

,如果您使用的是MVVM的發展模式,那麼你會做這樣的事情:

<Border Background="{Binding BackgroundColor}" ...

。 ..並在您的ViewModel中創建一個屬性,該屬性爲您提供了顏色刷螞蟻。然後,關於該邊界顏色的所有決策邏輯可以駐留在數據旁邊。

public double Y  
    {   
     get { return underlyingModel.Y; }   
     set { underlyingModel.Y = value; 
       // here, test underlyingModel.Y, and 
       // set backgroundColor private property, and 
       // raise the PropertyChanged event on "BackgroundColor" 
      }  
    }  

private Brush backgroundColor; 
public Brush BackgroundColor { 
     get { return backgroundColor; } 
     set { // whatever you want to do here, probably just 
       backgroundColor=value; 
       OnPropertyChanged("BackgroundColor")}; 
      } 
     } 
+0

我很難理解它。當值改變時,我需要能夠更新顏色。我更新了我的初始代碼以顯示我的ViewModel代碼。我可以在那裏設置屬性值。我不確定當值更新時如何工作。再次感謝你! – vladc77 2011-03-21 03:22:47

+0

然後,您實際上必須在您的ViewModel上擁有Brush屬性(或任何與Silverlight等價的功能;我通常在WPF中會這樣認爲),您將按照我剛剛添加的示例中給出的那樣綁定更多或更少的屬性。 – 2011-03-21 06:19:22

+0

非常感謝您的示例代碼。儘管我在Silverlight中提供了ChangePropertyAction,但我更喜歡你的方法。我會盡力實現你的方式。再次感謝你!!! – vladc77 2011-03-21 07:14:40

8

我不是真的把顏色放進你的viewmodel的愛好者。在我看來,這是最好使用一個轉換器,然後像這樣:

public class CoordinateToColorConverter : IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     return new SolidColorBrush((int) value == 2 ? Colors.Red : Colors.Black); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

    #endregion 
} 

然後你可以定義你的綁定是這樣的:

    <Border Background="{Binding Y, Converter={StaticResource CoordinateToColorConverter}}" Width="10" Height="18" VerticalAlignment="Center" Margin="0,0,10,0" /> 
+0

非常感謝你的努力。我需要評估我的情況。我需要能夠根據綁定中的某個值更改顏色。可以說這個值是「2」,這個方框會變成紅色。在其他情況下,盒子的顏色會變黑。我高度讚賞你的時間。 – vladc77 2011-03-21 07:19:30

2

我在最近與其他人討論。我個人使用Luc Bos提供的技術,但有另一種技術:RYODTS或滾動您自己的DataTemplateSelector。

這可以用很少的努力完成。一個例子可以在CodeProject上找到。

+0

不錯,使得代碼更具可讀性,因爲更多的設計標記位於XAML中。 – 2011-03-21 07:49:34

+0

感謝您的鏈接。我同意,這是很好的來源! – vladc77 2011-03-21 08:40:06

+0

我唯一的問題是,您需要爲每個不同的用途創建子類。我試圖想辦法在我的考試之間進行補救,但我承認這不是優先考慮的事情。 – 2011-03-21 10:18:59