将 WPF 扩展工具包 ColorPicker SelectedColor 属性绑定到

我正在尝试将SelectedColorWPF Extended Toolkit ColorPicker的属性绑定到DependencyPropertywith type SolidColorBrush。然后应该将此颜色用作位于画布上/画布中的形状的填充颜色。

这不起作用,据我所知,问题在于SelectedColorColorPicker的属性实际上是一个 type 的对象Color?。我的问题是,我如何让它发挥作用?如果我只是通过连接到事件的方法在代码中执行此操作,则可以使用SelectedColor.Value,但 AFAIK 不是 XAML 中的选项。

我确实尝试使用常规属性并使该类继承自INotifyPropertyChanged(如SelectedColor 绑定不会从 ColorPicker 更新为 Model),而不是 from DependencyObject,但这也不起作用。

我更喜欢用 Binding 来做这件事,因为我试图在使用 MVVM 的实践中获得更多。我最近才学会了如何使用 MVVM,而且我现在更喜欢它,所以我真的很想避免只使用代码隐藏。

XAML:

    <toolkit:ColorPicker x:Name="fillColor" Grid.Column="1" Grid.Row="8" Margin="5" VerticalAlignment="Top" SelectedColor="{Binding Path=FillColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedColorChanged="fillColor_SelectedColorChanged" />
    <toolkit:ColorPicker x:Name="lineColor" Grid.Column="2" Grid.Row="8" Margin="5" VerticalAlignment="Top" />


慕尼黑8549860
浏览 356回答 1
1回答

慕码人8056858

我认为 aConverter是解决您的麻烦的答案。下面是一个转换器类的例子:using System;using System.Globalization;using System.Windows.Data;namespace SO_app.Converters{&nbsp; &nbsp; public class DebugConverter : IValueConverter&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public object Convert(object value, Type targetType, object parameter, CultureInfo culture)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (null == value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // For a more sophisticated converter, check also the targetType and react accordingly..&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value is Color) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color color = (Color)value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new SolidColorBrush(color);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You can support here more source types if you wish&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // For the example I throw an exception&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type type = value.GetType();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new InvalidOperationException("Unsupported type ["+type.Name+"]");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}&nbsp;&nbsp;然后在你的 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" />&nbsp;&nbsp;注意:类型转换的代码来自这个SO post。更新:检查此 SO 帖子,这意味着工具包中有一个内置转换器
打开App,查看更多内容
随时随地看视频慕课网APP