开发者

WPF Binding simple question with class in Codebehind

I have a WPF control with a class in codebehind.

Public Class SimpleDrawingPlugin
    Implements PluginInterface.IPluginControl
    Private _PluginInfo As New PluginInterface.clsPluginBase


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _PluginInfo.Name = "Simple drawing "
        _PluginInfo.Description = "Drawing of circles and rectangles"
        _PluginInfo.Icon = _PluginInfo.BitmapToBitmapImage(My.Resources.SimpleDrawing)
 开发者_运维百科       _PluginInfo.Vendor = "Timo Böhme, 2011"
        _PluginInfo.FillColor = Colors.Orange '<-- Property to set to control

        Me.Ellipse1.DataContext = Me.PluginInfo '<-- Binding this Class
    End Sub


    Public ReadOnly Property PluginInfo As PluginInterface.IAdvancedControl Implements PluginInterface.IPluginControl.PluginInfo
        Get
            Return Me._PluginInfo
        End Get
    End Property
End Class

And XAML:

<UserControl x:Class="SimpleDrawingPlugin"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Ellipse x:Name="Ellipse1" >
            <Ellipse.Stroke>
                <SolidColorBrush  Color="Red"/>
            </Ellipse.Stroke>
            <Ellipse.Fill>
                <SolidColorBrush Color="{Binding Path=FillColor}"/> <!-- Does not work -->
            </Ellipse.Fill>
        </Ellipse>
    </Grid>
</UserControl>

The DataBinding with "Path=FillColor" does not work and does not update any color value in the control. What syntax is recommended to Bind the color to any own Class Property in Codebehind?

Edit if I use the following code, color stays Orange and will not change into yellow.

Private Sub SimpleDrawingPlugin_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    _PluginInfo.FillColor = Colors.Yellow
End Sub


I would replace FillColor as Color with FillBrush as SolidColorBrush. Then do this:

_PluginInfo.FillBrush = new SolidColorBrush(Colors.Orange)

Then in your xaml:

<Ellipse x:Name="Ellipse1" Stroke="Red" Fill="{Binding FillBrush}" />


The SolidColorBrush does not have a DataContext property, so it's not going to inherit the Ellipse's DataContext. You would need to do something like:

<SolidColorBrush Color="{Binding Path=DataContext.FillColor, ElementName=Ellipse1}"/>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜