2011-03-25 209 views
0

无论我在互联网上找到多少类似的解决方案,我都无法想象这个问题。这是我的问题。WPF数组属性绑定

我在我的WPF UserControl(MyControl)中有一个Brushes []属性。我希望能够使用几个静态定义的画笔来设置此控件的实例。我在想,XAMl看起来像

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

带有MyControl的.cs文件包含此Gem。在某些时候,我正在使用画笔绘制一些东西。

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[]{})); 

那么,你可以想象到目前为止没有任何工作。对于一些指向正确方向的人来说非常有必要。

回答

1

你应该能够只绑定Brushes到myBrushes这样

<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

非常感谢您! – Gleno 2011-03-25 19:27:21