将样式设置器绑定到公共财产?
我想将样式中的 Fill
属性绑定到公共属性,我使用了以下方法,但似乎不起作用。
public SevenSegmentControl()
{
InitializeComponent();
SevenSegmentColor = Color.FromRgb(251, 23, 23);
}
public Color SevenSegmentColor { get; set; }
<Style x:Key="RectangleStyle1" TargetType="{x:Type Rectangle}">
<Setter Property="Fill" Value="{Binding Path=SevenSegmentColor, Mode=TwoWay}"/>
<Setter Property="RadiusX" Value="4"/>
<Setter Property="RadiusY" Value="4"/>
<Setter Property="StrokeThickness" Value="0"/>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
</Style>
我该怎么办?
I want to bind the Fill
property in a style to a public property, I've used the following way but it seems like it doesn't work.
public SevenSegmentControl()
{
InitializeComponent();
SevenSegmentColor = Color.FromRgb(251, 23, 23);
}
public Color SevenSegmentColor { get; set; }
<Style x:Key="RectangleStyle1" TargetType="{x:Type Rectangle}">
<Setter Property="Fill" Value="{Binding Path=SevenSegmentColor, Mode=TwoWay}"/>
<Setter Property="RadiusX" Value="4"/>
<Setter Property="RadiusY" Value="4"/>
<Setter Property="StrokeThickness" Value="0"/>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
</Style>
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您试图将颜色值直接设置为 Fill 属性,其类型为 Brush。您可以检查“输出”窗口以找出 绑定错误。要么您需要 valueconverter 将颜色转换为有效的画笔,或者您需要这样做
编辑
如果您在设置属性值之前分配 Datacontext,则UI 永远不会在您的属性更改中收到通知。在您的情况下,您使用分配数据上下文,
因此在为
SevenSegmentColor
属性分配值之前,在初始化时间本身设置 Datacontext。稍后在初始化后您为属性分配了颜色值,UI 永远不会收到通知,因此您的颜色不会显示在 UI 中。要解决此问题,您需要实现 INotifyPropertyChanged 用户控件中的界面示例
You are trying to set a color value directly to the Fill property,which is of type Brush.You can inspect the Output window to find out the binding errors.Either you need a valueconverter to convert your color to a valid brush or you need to do like this
EDIT
If you are assigning the Datacontext before the Property value is set,the UI will never be notified in your Property change.As in your case you are assigning the datacontext using
So the Datacontext is set at the initialization time itself before your
SevenSegmentColor
property is assigned a value.Later after initialization when you assigned a color value to your Property, the UI will never get notified and hence your color is not shown in the UI.To solve this you need to implement the INotifyPropertyChanged Interface in your UserControlSample