本文介绍了将样式设置器绑定到公共属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将样式中的 Fill 属性绑定到公共属性,我使用了以下方法,但似乎不起作用.

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>

我该怎么做?

推荐答案

您正在尝试将颜色值直接设置为 Fill 属性,属于 Brush 类型.您可以检查输出窗口以找出 绑定错误.要么你需要一个valueconverter 将您的颜色转换为有效的画笔,或者您需要这样做

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

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SevenSegmentColor =new SolidColorBrush(Color.FromRgb(251, 23, 23));
            DataContext=this;
        }

        public SolidColorBrush SevenSegmentColor { get; set; }

    }

编辑

如果您在设置属性值之前分配数据上下文,则在您的属性更改中将永远不会通知 UI.在您的情况下,您正在使用

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

DataContext="{Binding RelativeSource={RelativeSource Self}}"

所以 Datacontext 在初始化时间设置在您的 SevenSegmentColor 属性被分配一个值之前.在初始化之后,当您为您的属性分配一个颜色值时,UI 将永远不会得到通知,因此您的颜色未显示在 UI 中.要解决此问题,您需要实施 INotifyPropertyChanged 用户控件中的接口

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 UserControl

示例

 /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private SolidColorBrush sevenSegmentColor;
        public MainWindow()
        {
            InitializeComponent();
           SevenSegmentColor =new SolidColorBrush(Color.FromRgb(251, 23, 23));
        }

        public SolidColorBrush SevenSegmentColor
        {
            get
            {
                return sevenSegmentColor;
            }
            set
            {
                sevenSegmentColor = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("SevenSegmentColor");
            }
        }

        // Declare the event
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }

这篇关于将样式设置器绑定到公共属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:07