1 回答
![?](http://img1.sycdn.imooc.com/5458692c00014e9b02200220-100-100.jpg)
TA贡献1803条经验 获得超6个赞
我认为 aConverter是解决您的麻烦的答案。下面是一个转换器类的例子:
using System;
using System.Globalization;
using System.Windows.Data;
namespace SO_app.Converters
{
public class DebugConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (null == value) {
return null;
}
// For a more sophisticated converter, check also the targetType and react accordingly..
if (value is Color) {
Color color = (Color)value;
return new SolidColorBrush(color);
}
// You can support here more source types if you wish
// For the example I throw an exception
Type type = value.GetType();
throw new InvalidOperationException("Unsupported type ["+type.Name+"]");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
}
然后在你的 xaml 中它看起来像这样:
这是我的项目:
xmlns:converter="clr-namespace:SO_app.Converters"
然后在资源中:
<converter:DebugConverter x:Key="DebugConverter"/>
然后在xaml中:
<toolkit:ColorPicker x:Name="fillColor" Grid.Column="1" Grid.Row="8" Margin="5" VerticalAlignment="Top" SelectedColor="{Binding Path=FillColor, Converter={StaticResource DebugConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedColorChanged="fillColor_SelectedColorChanged" />
注意:
类型转换的代码来自这个SO post。
更新:
检查此 SO 帖子,这意味着工具包中有一个内置转换器
- 1 回答
- 0 关注
- 362 浏览
添加回答
举报