-1

Foreground and Background properties are of Brush type. How to get Color for further usage in Color Picker?

I pass Foreground and Background to c-tor of a properties window like this:

private void Menu_Properties_OnClick(object sender, RoutedEventArgs e)
{
    PropertiesDialog propDialog = new PropertiesDialog(TbTimer.Foreground, TimerPanel.Background);
    if (propDialog.ShowDialog() == true)
    {
        //set color to TbTimer textbox and TimerPanel WrapPanel
    }
}

TbTimer is TextBox, TimerPanel is WrapPanel.

This is properties window c-tor:

public PropertiesDialog(Brush foreground, Brush background)
{
    InitializeComponent();

    FontColorPicker.SelectedColor = foreground; //Compilation error
    BgColorPicker.SelectedColor = background; //Compilation error
}

Here I get Cannot implicitly convert type 'System.Windows.Media.Brush' to 'System.Windows.Media.Color'.

How to get brushes color?

Dragon
  • 2,431
  • 11
  • 42
  • 68

3 Answers3

1

When you set the Background property of an element in XAML to something like "Red", a type converter converts the string "Red" into a SolidColorBrush object whose Color property is set to the appropriate colour.

So this:

<TextBlock Background="Red" />

is equivalent to this:

<TextBlock>
    <TextBlock.Background>
        <SolidColorBrush Color="Red" />
    </TextBlock.Background>
</TextBlock>

SolidColorBrush is the only brush type that has a single "colour". Other brush types may have more than one colour, or may not really represent "colour" at all (such as a DrawingBrush or VisualBrush for example).

If you want your dialog's constructor to take Brush objects as parameters but also want to deal with them as single colours, then you will need to cast them as SolidColorBrush objects as per @BenjaminPaul's answer. The type of the brush may actually be different of course, so an explicit cast would fail with an exception.

The best you can do is something like this:

public PropertiesDialog(Brush foreground, Brush background)
{
    InitializeComponent();

    var solidForeground = foreground as SolidColorBrush;
    var solidBackground = background as SolidColorBrush;

    if (solidForeground == null || solidBackground == null)
    {
        // One or both of the brushes does not have a
        // single solid colour; what you do here is up to you
        throw new InvalidOperationException();
    }

    FontColorPicker.SelectedColor = solidForeground.Color;
    BgColorPicker.SelectedColor = solidBackground.Color;
}
Steven Rands
  • 5,160
  • 3
  • 27
  • 56
0

simpy alter your method signature:

public PropertiesDialog(System.Windows.Media.Color foreground, System.Windows.Media.Color background)
{
    InitializeComponent();

    FontColorPicker.SelectedColor = foreground; 
    BgColorPicker.SelectedColor = background; 
}
apomene
  • 14,282
  • 9
  • 46
  • 72
0

SelectedColour is expecting the System.Windows.Media.Color type however you are attempting to assign a brush... try casting as follows.

public PropertiesDialog(Brush foreground, Brush background)
{
    InitializeComponent();

    FontColorPicker.SelectedColor = ((SolidColorBrush)foreground).Color;
    BgColorPicker.SelectedColor = ((SolidColorBrush)background).Color; 
}
BenjaminPaul
  • 2,931
  • 19
  • 18