我很抱歉问一个非常基本的问题,但我正在努力在xaml页面后面的代码中使用转换器。此转换器将根据数据元素中的值更改背景颜色。我知道我想要的数据元素是可用的,因为当我在执行转换器时中断调试时,我想要的值是" value"。在VS2013的即时窗口中,我显示"值"的内容。得到这个:
{VehicleTracks.ViewModels.vehicleViewModel}
base: {VehicleTracks.ViewModels.vehicleViewModel}
purchasedate: {7/14/2014 12:00:00 AM}
PurchaseDate: {7/14/2014 12:00:00 AM}
VehColor: "Chartreuse"
VehicleBitMap: null
vehiclebitmap: null
vehiclecolor: "Chartreuse"
vehiclemodel: "Algo"
vehmake: "Test Vehicle"
VehMake: "Test Vehicle"
VehType: "Auto"
vehtype: "Auto"
vehyear: "1908"
VehYear: "1908"
元素" VehType"是我想要的。我想根据其值更改背景颜色。你看它被设置为" Auto"在这种情况下。以下是我的转换器代码的适用部分:
public sealed class ValueToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
SolidColorBrush blueBrush = new SolidColorBrush(Windows.UI.Colors.Blue);
if (value[VehType] == "Auto")
{
return blueBrush;
}
else
{
SolidColorBrush greenBrush = new SolidColorBrush(Windows.UI.Colors.Green);
return greenBrush;
}
}
我现在有参考,值[VehType]没有得到它。那我需要什么?
答案 0 :(得分:4)
您在那里滥用索引器,这里有一篇文章可以帮助解释:Using Indexers in C#
你能不能把它投到VehicleTracks.ViewModels.vehicleViewModel
?
using VehicleTracks.ViewModels;
// -----
var viewModel = (vehicleViewModel) value;
if (viewModel.VehType == "Auto")
{
return blueBrush;
}