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
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))));
I would probably hold the RGB values as dependency properties, you can alter them from code behind as you please then.
Results
XAML
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:test"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid>
<Grid.Background>
<MultiBinding Converter="{local:RGBtoBrushConverter}">
<MultiBinding.Bindings>
<Binding Path="R"/>
<Binding Path="G"/>
<Binding Path="B"/>
</MultiBinding.Bindings>
</MultiBinding>
</Grid.Background>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding R, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Column="1" Text="{Binding G, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Column="2" Text="{Binding B, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Grid>
</Window>
Code
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test
{
public class RGBtoBrushConverter : MarkupExtension, IMultiValueConverter
{
static RGBtoBrushConverter converter;
public RGBtoBrushConverter() { }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (converter == null) converter = new RGBtoBrushConverter();
return converter;
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
try
{
byte R = System.Convert.ToByte(values[0]);
byte G = System.Convert.ToByte(values[1]);
byte B = System.Convert.ToByte(values[2]);
return new System.Windows.Media.SolidColorBrush(Color.FromArgb(byte.MaxValue, R, G, B));
}
catch { return Brushes.Purple; }
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public partial class MainWindow : Window
{
public int R
{
get { return (int)GetValue(RProperty); }
set { SetValue(RProperty, value); }
}
public static readonly DependencyProperty RProperty = DependencyProperty.Register("R", typeof(int), typeof(MainWindow), new PropertyMetadata(0));
public int G
{
get { return (int)GetValue(GProperty); }
set { SetValue(GProperty, value); }
}
public static readonly DependencyProperty GProperty = DependencyProperty.Register("G", typeof(int), typeof(MainWindow), new PropertyMetadata(0));
public int B
{
get { return (int)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
public static readonly DependencyProperty BProperty = DependencyProperty.Register("B", typeof(int), typeof(MainWindow), new PropertyMetadata(0));
public MainWindow()
{
InitializeComponent();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With