0

I can't seem to figure this one out, no matter how much similar solutions I find on the internet. Here's my problem.

I have a Brushes[] property in my WPF UserControl (MyControl). I want to be able to style instances of this control with several statically defined brushes. I was thinking that the XAMl would looks something like

<Snip>
    <Window.Resources>
    <Color x:Key="ColorA">#304B82</Color>
    <Color x:Key="ColorB">#F3F3F3</Color>

    <x:ArrayExtension Type="Brush" x:Key="myBrushes">
    <SolidColorBrush Color="{StaticResource ColorA}"/>
    <SolidColorBrush Color="{StaticResource ColorB}"/>
    </x:ArrayExtension>

    <Style>
       //Magic here to apply myBrushes to the Brushes array
    </Style>

    </Window.Resources>


    <MyNamespace:MyControl>
    </MyNamespace:MyControl>
<Snap>

The .cs File with MyControl holds this gem. At some point I'm using Brushes to draw something.

    public Brush[] Brushes
    {
        get { return (Brush[])GetValue(BrushesProperty); }
        set { SetValue(BrushesProperty, value); }
    }

    public static readonly DependencyProperty BrushesProperty = DependencyProperty.Register(
      "Brushes", typeof(Brush[]), typeof(MyControl), new PropertyMetadata(new Brush[]{}));

Well, as you can imagine absolutely nothing is working so far. Would be much obliged for some pointers in the right direction.

1 Answer 1

1

You should be able to just bind Brushes to myBrushes like this

<Window.Resources>
    <Color x:Key="ColorA">#304B82</Color>
    <Color x:Key="ColorB">#F3F3F3</Color>
    <x:Array Type="Brush" x:Key="myBrushes">
        <SolidColorBrush Color="{StaticResource ColorA}"/>
        <SolidColorBrush Color="{StaticResource ColorB}"/>
    </x:Array>
    <Style TargetType="{x:Type my:MyControl}">
        <Setter Property="Brushes"
                Value="{Binding Source={StaticResource myBrushes}}"/>
    </Style>
</Window.Resources>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.