将样式设置器绑定到公共财产?

发布于 2024-11-08 17:45:48 字数 691 浏览 0 评论 0原文

我想将样式中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧夏天 2024-11-15 17:45:48

您试图将颜色值直接设置为 Fill 属性,其类型为 Brush。您可以检查“输出”窗口以找出 绑定错误。要么您需要 valueconverter 将颜色转换为有效的画笔,或者您需要这样做

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

        public SolidColorBrush SevenSegmentColor { get; set; }

    }

编辑

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

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

因此在为 SevenSegmentColor 属性分配值之前,在初始化时间本身设置 Datacontext。稍后在初始化后您为属性分配了颜色值,UI 永远不会收到通知,因此您的颜色不会显示在 UI 中。要解决此问题,您需要实现 INotifyPropertyChanged 用户控件中的界面

示例

 /// 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));
            }
        }
    }

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; }

    }

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

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

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

Sample

 /// 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));
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文