MVVM 和 UniformGrid 数据绑定

发布于 2024-10-08 09:21:45 字数 732 浏览 3 评论 0原文

我正在尝试使用一些矩形来设置 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 的矩形? UniformGrid 上没有“ItemsSource”属性。我需要使用 ItemsControl 吗?如果是这样,我该怎么做?

提前致谢。

I'm trying to style the back of a WPF chart with some rectangles. I'm using MVVM, and I need the rectangles to be uniformly sized. When defined via Xaml, this works with a fixed "BucketCount" of 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>

How can I bind my ObservableCollection of Rectangles? There is no "ItemsSource" property on UniformGrid. Do I need to use an ItemsControl? If so, how can I do this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2024-10-15 09:21:45

您可以像这样使用 ItemsControl 进行绑定。简单示例,其中 ItemsSource 只是一个 ObservableCollection

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

Update
它适用于我的使用场景,但我可能在这里遗漏了一些东西。这是我尝试过的完整代码。 得到相同的结果

我从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;
    }
}

You could use ItemsControl to Bind like this. Simple example where ItemsSource is just an 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>

Update
It works for my usage scenario, but I might be missing something here. Here's the full code I've tried. I get the same result from both

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文