WPF ColorPicker from wpf/xaml/toolkit 绑定到属性错误

本文关键字:绑定 属性 错误 toolkit xaml ColorPicker from wpf WPF | 更新日期: 2023-09-27 17:55:46

我有一些带有颜色选择器的代码:

<xctk:ColorPicker Grid.Row ="10" Grid.ColumnSpan="2" Margin="5, 5, 5, 5" Height="30" DisplayColorAndName="True"
                          SelectedColor="{Binding SelectedItem.TransparentColor, ElementName=ItemsListBox, Converter={StaticResource BrushColorConverter}, Mode=TwoWay}"/>

当绑定到元素名称=窗口和路径=背景时,这个东西可以工作,但是当我使用属性System.Windows.Media.Color创建对象时,它会显示

Value produced by BindingExpression is not valid for target property.; Value='<null>' BindingExpression:Path=SelectedItem.TransparentColor; DataItem='ListBox' (Name='ItemsListBox'); target element is 'ColorPicker' (Name=''); target property is 'SelectedColor' (type 'Color')

属性 TransparentColor 是新对象 ( new System.Windows.Media.Color() )...我应该怎么做才能让它工作?

其他事情,如:

<TextBox Grid.Column="7" Text="{Binding SelectedItem.ForbiddenArea.Height, ElementName=ItemsListBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

工程。。。

WPF ColorPicker from wpf/xaml/toolkit 绑定到属性错误

很容易修复:首先,我已经从System.Windows.Media声明了可为空的颜色

Color? TransparentColor { get; set; }

并在没有转换器的情况下绑定到此属性:

<xctk:ColorPicker Grid.Row ="12" Grid.ColumnSpan="2" Height="30" DisplayColorAndName="True"
                          SelectedColor="{Binding TransparentColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          IsEnabled="{Binding ElementName=GlobalTransparencyFlag, Path=IsChecked}"/>