简体   繁体   中英

WPF ColorPicker from wpf/xaml/toolkit binding to property error

I've got some code with 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

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?

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

Color? TransparentColor { get; set; }

And Binding to this property without 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}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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