使用UniformGrid进行MVVM和数据绑定

时间:2010-12-20 21:27:49

标签: wpf silverlight xaml data-binding mvvm

我正在尝试用一些矩形设计WPF图表的背面。我正在使用MVVM,我需要将矩形统一调整大小。当通过Xaml定义时,这适用于固定的“BucketCount”4:

<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的Rectangles? UniformGrid上没有“ItemsSource”属性。我需要使用ItemsControl吗?如果是这样,我该怎么做?

提前致谢。

1 个答案:

答案 0 :(得分:8)

您可以使用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;
    }
}