【发布时间】: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