开发者

How to change a borders background with a colorpicker

i have a xaml resource file with some definitions of DataRemplates. In one DataRemplate I have a border background bound to a property of its DataObject.

I also have a ColorPicker user control which is part of the ContextMenu of the border.

Now I'm trying to bind the dependency property "CustomColor" of the ColorPicker to the color/background property of the border. How to do this?

Do i have to bind to the "Background" property of the border or to the "Color" property of my dataobject?

开发者_JS百科
<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>

Thanks for your replies.


Both properties should be bound to the Color in your data-object, to make sure the Border is updated if the ColorPicker changes the property the dataobject needs to implement INotifyPropertyChanged.

How you do the bindings depends on your context. In most cases dataobjects are found in the DataContext of your respective object but with ItemsControls and the like that may not be the case. For general help with bindings see the Data Binding Overview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜