3

How to bind Color Bkg (System.Drawing.Color), defined in settings, with Style in XAML?

xmlns:props="clr-namespace:App.Properties"

<Style TargetType="{x:Type StackPanel}" x:Key="_itemStyle">
     <Setter Property="Background" Value="{Binding Path=Bkg, Source={x:Static props:Settings.Default}}"/>

Background property is of type System.Windows.Media.Color, so it needs to be somehow converted?

Patrik Polakovic
  • 452
  • 1
  • 5
  • 16
  • Why not simply create a setting of type `System.Windows.Media.Color`? Just click `Browse...` in the Settings Type combobox and select PresentationCore -> System.Windows.Media -> Color. – Clemens Jul 24 '13 at 12:01
  • Hmm, nice. But how to define transparent color? – Patrik Polakovic Jul 24 '13 at 13:08
  • @PatrikPolakovic if any ans solve your problem the you can mark it right .. – loop Jul 24 '13 at 13:19
  • @PatrikPolakovic Just set the first byte according to the desired opacity, or alpha value. The structure of the color string is #AARRGGBB, hence e.g. #7FFF0000 is semi-transparent red. See [here](http://msdn.microsoft.com/en-us/library/system.windows.media.color.aspx) also. – Clemens Jul 24 '13 at 13:35
  • In addition to what dkozl has answered, you may also directly create a setting of type `System.Windows.Media.SolidColorBrush`. – Clemens Jul 24 '13 at 13:42
  • @PatrikPolakovic i think that you got your ans..so whichever solved your problem you can atleast mark that right.. – loop Jul 25 '13 at 09:49
  • Thank you for your help. Problem solved using @dkozl solution :) – Patrik Polakovic Jul 25 '13 at 11:37

3 Answers3

6

Panel.Background property is of a System.Windows.Media.Brush type and not System.Windows.Media.Color therefore you need to convert it into SolidColorBrush. Below you can find both case scenarios:

Setting is of System.Windows.Media.Color type

<Setter Property="Background">
   <Setter.Value>
      <SolidColorBrush Color="{Binding Source={x:Static props:Settings.Default}, Path=Bkg}"/>
   </Setter.Value>
</Setter>

Setting is of System.Drawing.Color type: for this you need custom IValueConverter to convert it into SolidColorBrush:

public class ColorToBrushConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
      var dc = (System.Drawing.Color)value;
      return new SolidColorBrush(new Color { A = dc.A, R = dc.R, G = dc.G, B = dc.B });
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
      throw new NotImplementedException();
  }
}

which you define in your resources:

<Window.Resources>
    <local:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
</Window.Resources>

and you can use it like this:

<Setter Property="Background" Value="{Binding Source={x:Static props:Settings.Default}, Path=Bkg, Converter={StaticResource ColorToBrushConverter}}"/>
dkozl
  • 30,579
  • 7
  • 72
  • 80
0

As you would know that background property is of solidbrush type so its value can be set or get only with some solidbrush typw property . so what you can do is make a solidbrush type property in place of color like this..in your setting class. and now every thing just work fine..

 static SolidColorBrush brush = new SolidColorBrush(Colors.Red);

    public static SolidColorBrush colorBrush
    {
        get
        {
            return brush;
        }
    }

if you dont want to do that then you have to use value converter ..for that you can follow

this link..hope it helps you..

Community
  • 1
  • 1
loop
  • 8,162
  • 9
  • 34
  • 75
0

Simply create a setting of type System.Windows.Media.SolidColorBrush.

Select Browse... from the Type ComboBox of the new setting, then select PresentationCore -> System.Windows.Media -> SolidColorBrush.

You may now directly use that setting as you already did:

<Setter Property="Background"
        Value="{Binding Path=Bkg, Source={x:Static props:Settings.Default}}"/>
Clemens
  • 110,214
  • 10
  • 133
  • 230