SetCurrentValue myLabel.BackgroundProperty without breaking the binding

binding, c#, wpf, xaml

Solution

The error is self desciptive, it is tell you the lbl.BackgroundProperty can't be set that way. The below is the right way to do this.

        lbl.SetCurrentValue(BackgroundProperty,
                                   new SolidColorBrush(Color.FromRgb(System.Convert.ToByte(121),
                                                                     System.Convert.ToByte(43),
                                                                     System.Convert.ToByte(15))));

Problem

I have three textboxes and a label. Label is bound to the text in textboxes using Multiconverter. XAML: ``` <Label Name="lbl" Content="Label" HorizontalAlignment="Left" Margin="336,128,0,0" VerticalAlignment="Top" Height="57" Width="93"> <Label.Background> <MultiBinding Converter="{StaticResource converta}"> <Binding ElementName="R" Path="Text" Mode="TwoWay" /> <Binding ElementName="G" Path="Text" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" /> <Binding ElementName="B" Path="Text" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" /> </MultiBinding> </Label.Background> </Label> <TextBox Name="R" HorizontalAlignment="Left" Height="23" Margin="250,214,0,0" TextWrapping="Wrap" Text="255" VerticalAlignment="Top" Width="120"/> <TextBox Name="B" HorizontalAlignment="Left" Height="23" Margin="271,242,0,0" TextWrapping="Wrap" Text="255" VerticalAlignment="Top" Width="120"/> <TextBox Name="G" HorizontalAlignment="Left" Height="23" Margin="155,275,0,0" TextWrapping="Wrap" Text="255" VerticalAlignment="Top" Width="120"/> ``` Converter ``` class converter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return new System.Windows.Media.SolidColorBrush(Color.FromRgb(System.Convert.ToByte((values[0] as string)), System.Convert.ToByte((values[1] as string)), System.Convert.ToByte((values[2] as string)))); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { byte R = (value as System.Windows.Media.Color?).Value.R; byte G = (value as System.Windows.Media.Color?).Value.G; byte B = (value as System.Windows.Media.Color?).Value.B; return new string[] {System.Convert.ToString(R),System.Convert.ToString(G),System.Convert.ToString(B)}; } } ``` (Obviously, in a real world i'd add validation and type checks). Now i want to add a button that would set the label background, something simple like this: ``` lbl.Background= new SolidColorBrush(Color.FromRgb( System.Convert.ToByte(121), System.Convert.ToByte(43), System.Convert.ToByte(15))); ``` Obviously, it breaks the binding. So i try: ``` SetCurrentValue(lbl.BackgroundProperty, new SolidColorBrush( Color.FromRgb(System.Convert.ToByte(121), System.Convert.ToByte(43), System.Convert.ToByte(15)))); ``` But VS complains that Member 'System.Windows.Controls.Control.BackgroundProperty' cannot be accessed with an instance reference; qualify it with a type name instead How can i set the value of the background of the label in the code without breaking the binding? Note: This is WPF, .Net 4.5

Original source