gpt4 book ai didi

具有动态列的 wpf 网格

转载 作者:行者123 更新时间:2023-12-04 11:27:08 28 4
gpt4 key购买 nike

我有一个希望绑定(bind)到 WPF 网格的集合。

我面临的问题是列数是动态的并且取决于集合。这是一个简单的模型:

public interface IRows
{
string Message{get;}
IColumns[] Columns{get;}
}

public interface IColumns
{
string Header {get;}
AcknowledgementState AcknowledgementState{get;}
}

public interface IViewModel
{
ObservableCollection<IRows> Rows {get;}
}

我希望我的 View 绑定(bind)到包含列集合的行集合。

我的 Columns 集合包含一个应该由图像表示的枚举(3 种可能性中的 1 种)。它还包含一个 Message 属性,该属性只应显示在一列中(静态的,只是一些文本信息)。它还包含一个标题字符串,该字符串应显示为该列的标题。

The link to what I want to show

请注意,列数是可变的(此时标题设置为确认,但这将更改为表示动态数据)。

更新:这是在实现 Rachel 的建议之后
    <ItemsControl
ItemsSource="{Binding Items, Converter={StaticResource PresentationConverter}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="true"
local:GridHelpers.RowCount="{Binding RowCount}"
local:GridHelpers.ColumnCount="{Binding ColumnCount}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding RowIndex}"/>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type UI:MessageEntity}">
<TextBox Text="{Binding Message}"></TextBox>
</DataTemplate>
<DataTemplate DataType="{x:Type UI:StateEntity}">
<TextBox Text="{Binding State}"></TextBox>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

这几乎给了我现在想要的东西。我只坚持应该为标题做些什么。
欢迎任何建议。

最佳答案

您可以使用嵌套 ItemsControls为了这

这是一个基本示例:

<!-- Bind Rows using the default StackPanel for the ItemsPanel -->
<ItemsControl ItemsSource="{Binding Rows}">
<!-- Set the Template for each row to a TextBlock and another ItemsControl -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- Need to set Width of name TextBlock so items line up correctly -->
<TextBlock Width="200" Text="{Binding Name}" />

<ItemsControl ItemsSource="{Binding Columns}">
<!-- Use a horizontal StackPanel to display columns -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

关于具有动态列的 wpf 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13814224/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com