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

时间:2013-01-24 06:57:06

标签: c# wpf binding

我有一些使用colorpicker的代码:

<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}"/>

这个东西在Binded到ElementName = Window和Path = Background时有效,但是当我用属性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}"/>

工程...

1 个答案:

答案 0 :(得分:0)

很容易修复: 首先,我已经从System.Windows.Media

声明了可以为空的Color
Color? TransparentColor { get; set; }

在没有Converter的情况下绑定到此属性:

<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}"/>