【问题标题】:How to change a borders background with a colorpicker如何使用颜色选择器更改边框背景
【发布时间】:2011-03-29 08:58:36
【问题描述】:

我有一个 xaml 资源文件,其中包含DataRemplates 的一些定义。在一个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>

感谢您的回复。

【问题讨论】:

    标签: wpf xaml data-binding user-controls


    【解决方案1】:

    这两个属性都应该绑定到数据对象中的颜色,以确保在ColorPicker 更改数据对象需要实现INotifyPropertyChanged 的属性时更新边框。

    如何进行绑定取决于您的上下文。在大多数情况下,数据对象可以在您各自对象的DataContext 中找到,但使用 ItemsControls 等可能并非如此。有关绑定的一般帮助,请参阅Data Binding Overview

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 2020-05-19
      • 2019-10-10
      • 1970-01-01
      相关资源
      最近更新 更多