WPF 在代码隐藏中使用类绑定简单问题
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将
FillColor as Color
替换为FillBrush as SolidColorBrush
。然后这样做:然后在你的xaml中:
I would replace
FillColor as Color
withFillBrush as SolidColorBrush
. Then do this:Then in your xaml:
SolidColorBrush 没有 DataContext 属性,所以它不会继承 Ellipse 的 DataContext。你需要做类似的事情:
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: