WPF 在代码隐藏中使用类绑定简单问题

发布于 2024-11-08 01:57:59 字数 2326 浏览 1 评论 0原文

我有一个 WPF 控件,其代码隐藏中有一个类。

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

并且 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>

带有“Path=FillColor”的 DataBinding 不起作用,并且不会更新控件中的任何颜色值。建议使用什么语法将颜色绑定到代码隐藏中的任何自己的类属性?

编辑 如果我使用以下代码,颜色保持橙色并且不会变成黄色。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-11-15 01:57:59

我会将 FillColor as Color 替换为 FillBrush as SolidColorBrush。然后这样做:

_PluginInfo.FillBrush = new SolidColorBrush(Colors.Orange)

然后在你的xaml中:

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

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}" />
泪痕残 2024-11-15 01:57:59

SolidColorBrush 没有 DataContext 属性,所以它不会继承 Ellipse 的 DataContext。你需要做类似的事情:

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

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}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文