Gleno
Gleno

Reputation: 16949

WPF Array Property Binding

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.

Upvotes: 0

Views: 2506

Answers (1)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84647

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>

Upvotes: 1

Related Questions