【问题标题】:WPF Binding simple question with class in CodebehindWPF在Codebehind中用类绑定简单问题
【发布时间】:2011-05-17 22:00:53
【问题描述】:

我有一个 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 不起作用,并且不会更新控件中的任何颜色值。建议使用什么语法将颜色绑定到 Codebehind 中的任何自己的类属性?

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

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

【问题讨论】:

  • 天啊!我真的什么都没问?谢谢你的建议。我会编辑它...
  • 绑定将通过 DataContext 对象查找具有该名称的属性,您没有
  • 我以为这个是datacontext:Me.Ellipse1.DataContext = Me.PluginInfo,不是吗?还是错了?

标签: .net wpf vb.net xaml data-binding


【解决方案1】:

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

_PluginInfo.FillBrush = new SolidColorBrush(Colors.Orange)

然后在你的 xaml 中:

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

【讨论】:

    【解决方案2】:

    SolidColorBrush 没有 DataContext 属性,因此它不会继承 Ellipse 的 DataContext。您需要执行以下操作:

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

    【讨论】:

    • 嗨 CodeNaked。谢谢。我第一次看到现在的颜色。但是当我更改“PropertyInfo”类中的属性“FillColor”时,它不会更新控件。如何设置属性让它更新控件?
    • @goldengel - 您的 PropertyInfo 对象需要实现 INotifyPropertyChanged
    猜你喜欢
    • 2011-03-20
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多