简体   繁体   English

WPF ColorPicker从WPF / XAML /工具包绑定到属性错误

[英]WPF ColorPicker from wpf/xaml/toolkit binding to property error

I've got some code with colorpicker: 我有一些关于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}"/>

This stuff works when is Binded to ElementName=Window and Path=Background, but when I'm creating object with property System.Windows.Media.Color, it shows 当绑定到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')

Property TransparentColor is new object ( new System.Windows.Media.Color() )... What should I do to make it working? 属性TransparentColor是新对象(new System.Windows.Media.Color())...我应该怎么做才能使其正常工作?

Other things like: 其他类似:

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

works... 作品...

It was easy to fix: Firstly I've declared nullable Color from System.Windows.Media 修复起来很容易:首先,我从System.Windows.Media声明了可为空的Color

Color? TransparentColor { get; set; }

And Binding to this property without Converter: 并在没有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}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM