我有一个转换器类在Windows Store应用程序中:
namespace MyNamespace {
public class ColorToBrushConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, string language) {
if (value is Windows.UI.Color) {
Windows.UI.Color color = (Windows.UI.Color) value;
SolidColorBrush r = new SolidColorBrush(color);
return r;
}
CommonDebug.BreakPoint("Invalid input to ColorToBrushConverter");
throw new InvalidOperationException();
}
public object ConvertBack(object value, Type targetType, object parameter, string language) {
throw new NotImplementedException();
}
}
}
我现在正尝试在xaml中使用它。我无法想出正确的语法来告诉xaml使用我的转换器。
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" >
<Setter Property="Background" Value="{Binding Source=BackgroundColor, UpdateSourceTrigger=PropertyChanged, Converter=????????????????}"/>
</Style>
</ListView.ItemContainerStyle>
编辑:显然,Windows Store应用程序不允许开发人员使用在WPF中工作的所有数据绑定。这可能部分解释了我的问题。但我仍然不确定在Windows 8.1更新后是否会继续保持这种情况。
Source=BackgroundColor
更改为Path=BackGroundColor
,并确保您的视图模型中有一个名为 BackgroundColor 的属性。当我复制/粘贴您的代码时,我错过了这一点。Source
暗示要绑定到现有源对象,而Path
指定当前绑定对象上的路径(默认情况下是 DataContext)。也就是说,只有在您打算更改项目绑定的对象时才使用Source
。 - slugster