如何在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 + ")"; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不太喜欢在视图模型中添加颜色。在我看来,最好使用转换器,如下所示:
然后您可以像这样定义绑定:
I'm not really a fan of putting colors inside your viewmodel. In my opinion it is best to use a converter then like so:
you can then define your binding like this:
我最近正在和别人讨论这个问题。我个人使用了 Luc Bos 提供的技术,但还有另一种技术:RYODTS 或 Roll Your Own DataTemplateSelector。
这可以通过相对较少的努力来完成。可以在 CodeProject 上找到示例。
I was having this discussion with someone else recently. I personally used the technique Luc Bos provided but there is another technique: RYODTS or Roll Your Own DataTemplateSelector.
This can be done with relative little effort. An example can be found on CodeProject.
如果您使用 MVVM 开发模型,那么您将执行以下操作:
...并在 ViewModel 中创建一个属性,该属性提供你想要的颜色画笔。然后,有关该边框颜色的所有决策逻辑都可以驻留在您的数据旁边。
if you're using an MVVM development model, then you would do something like:
<Border Background="{Binding BackgroundColor}" ...
...and create a Property in your ViewModel which provides the color brush you want. Then, all the decision logic about that border color can reside next to your data.