2010-12-20 542 views
4

我試圖用一些矩形設計WPF圖表的樣式。我使用的是MVVM,我需要矩形的大小一致。當通過XAML中定義的,這個作品爲4的固定「BucketCount」:MVVM和與UniformGrid的數據綁定

<VisualBrush> 
    <VisualBrush.Visual> 
    <UniformGrid Height="500" Width="500" Rows="1" Columns="{Binding BucketCount}"> 
    <Rectangle Grid.Row="0" Grid.Column="0" Fill="#22ADD8E6" /> 
    <Rectangle Grid.Row="0" Grid.Column="1" Fill="#22D3D3D3"/> 
    <Rectangle Grid.Row="0" Grid.Column="2" Fill="#22ADD8E6"/> 
    <Rectangle Grid.Row="0" Grid.Column="3" Fill="#22D3D3D3"/> 
    </UniformGrid>   
</VisualBrush.Visual> 
<VisualBrush> 

我如何可以綁定我的矩形的的ObservableCollection? UniformGrid上沒有「ItemsSource」屬性。我需要使用ItemsControl嗎?如果是這樣,我該怎麼做?

在此先感謝。

回答

6

你可以像這樣使用ItemsControl綁定。簡單的例子,其中的ItemsSource只是一個ObservableCollection<Brush>

<VisualBrush> 
    <VisualBrush.Visual> 
     <ItemsControl x:Name="itemsControl" ItemsSource="{Binding MyBrushes}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Height="500" Width="500" Rows="1"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Rectangle Fill="{Binding}"/> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </VisualBrush.Visual> 
</VisualBrush> 

更新
它適合我的使用場景,但我可能失去了一些東西。這是我試過的完整代碼。我從兩個

MainWindow.xaml

<Grid> 
    <Grid.Background> 
     <VisualBrush> 
      <VisualBrush.Visual> 
       <ItemsControl x:Name="itemsControl" ItemsSource="{Binding MyBrushes}"> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <UniformGrid Height="500" Width="500" Rows="1"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Rectangle Fill="{Binding}"/> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
       <!--<UniformGrid Height="500" Width="500" Rows="1" Columns="4"> 
        <Rectangle Grid.Row="0" Grid.Column="0" Fill="#22ADD8E6" /> 
        <Rectangle Grid.Row="0" Grid.Column="1" Fill="#22D3D3D3"/> 
        <Rectangle Grid.Row="0" Grid.Column="2" Fill="#22ADD8E6"/> 
        <Rectangle Grid.Row="0" Grid.Column="3" Fill="#22D3D3D3"/> 
       </UniformGrid>--> 
      </VisualBrush.Visual> 
     </VisualBrush> 
    </Grid.Background> 
</Grid> 

MainWindow.xaml.cs

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     BrushConverter brushConverter = new BrushConverter(); 
     MyBrushes = new ObservableCollection<Brush>(); 
     MyBrushes.Add(brushConverter.ConvertFrom("#22ADD8E6") as Brush); 
     MyBrushes.Add(brushConverter.ConvertFrom("#22D3D3D3") as Brush); 
     MyBrushes.Add(brushConverter.ConvertFrom("#22ADD8E6") as Brush); 
     MyBrushes.Add(brushConverter.ConvertFrom("#22D3D3D3") as Brush); 
     this.DataContext = this; 
    } 

    public ObservableCollection<Brush> MyBrushes 
    { 
     get; 
     set; 
    } 
} 
+0

似乎並沒有得以順利相同的結果...我不知道我是什麼可能會失蹤? – 2010-12-21 02:22:54

+0

@JP:你沒有得到任何結果,或者它沒有按照它應有的方式工作? – 2010-12-21 08:10:06

+0

沒有結果。什麼都沒有出現。我檢查了綁定名稱,所以這不是問題。 – 2010-12-21 13:25:45