如何使用颜色选择器更改边框背景
问题描述:
我有一个xaml资源文件,其中包含一些定义DataRemplate
s。在一个DataRemplate
我有一个边界背景绑定到其DataObject
的属性。如何使用颜色选择器更改边框背景
我还有一个ColorPicker
用户控件,它是ContextMenu
边框的一部分。
现在我试图将ColorPicker
的依赖项属性“CustomColor”绑定到边框的颜色/背景属性。这个怎么做?
我是否必须绑定到边框的“背景”属性或我的数据对象的“颜色”属性?
<Border x:Name="projectRect"
Grid.Column="1"
Grid.ColumnSpan="1"
HorizontalAlignment="Right"
Background="{Binding Path=Color, Converter={StaticResource colorConverter}}"
BorderBrush="#737377"
BorderThickness="1"
CornerRadius="4"
IsHitTestVisible="True">
<TextBlock Text="{Binding Path=ProjectId}"
VerticalAlignment="Center"
Margin="4" />
<Border.ContextMenu>
<ContextMenu Name="colorPopup"
StaysOpen="True"
Style="{StaticResource ColorPickerContextMenuStyle}">
<Border Background="GhostWhite">
<local:CustomColorPicker x:Name="cp"
CustomColor="{Binding Path=Color, Converter={StaticResource colorConv}}"
diag:PresentationTraceSources.TraceLevel="High" />
</Border>
</ContextMenu>
</Border.ContextMenu>
</Border>
感谢您的回复。
答
这两个属性都应绑定到数据对象中的Color,以确保在ColorPicker
更改数据对象实现INotifyPropertyChanged
所需的属性时更新边框。
你如何做绑定取决于你的上下文。在大多数情况下,数据对象可在各自对象的DataContext
中找到,但ItemsControls等可能不是这种情况。有关绑定的一般帮助,请参阅Data Binding Overview。