开发者

WPF Array Property Binding

开发者 https://www.devze.com 2023-02-20 06:03 出处:网络
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 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 b开发者_如何学Pythonrushes. 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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消