wpf - MVVM 和 UniformGrid 的数据绑定(bind)

标签 wpf silverlight xaml data-binding mvvm

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

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

如何绑定(bind)我的 ObservableCollection 矩形? UniformGrid 上没有“ItemsSource”属性。我需要使用 ItemsControl 吗?如果是这样,我该怎么做?

提前致谢。

最佳答案

您可以像这样使用 ItemsControl 来绑定(bind)。 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;
    }
}

关于wpf - MVVM 和 UniformGrid 的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4494133/

相关文章:

WPF 数据模板 : How to create a tooltip just-in-time?

c# - WPF:如何将 OpenFileDialog 的结果绑定(bind)到已绑定(bind)的 TextBox.Text

c# - 使包装类的扩展方法/构造函数通用

c# - 使用来自不同方法的变量

c# - 在 Silverlight 中为 Bing map 设置最小/最大缩放

c# - Windows Phone 7 Mango 启动画面上的断断续续的渐变

WPF 声明使用声明的 xmlns 嵌套命名空间

c# - 工作 MvvM 和并发

c# - 在 Silverlight 项目中引用 .Web 项目时出现问题

xaml - 没有标题的 Windows Phone 7 全景项目