Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

c# - Binding Style Setter to a public property?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...