C# 每单元填充矩阵定义
我需要在类似矩阵的视图中显示和编辑我的数据。所有数据都将在一个C# 每单元填充矩阵定义,c#,wpf,matrix,grid,C#,Wpf,Matrix,Grid,我需要在类似矩阵的视图中显示和编辑我的数据。所有数据都将在一个ObserverableCollection中收集。然后我将按单元格定义填充集合。每个单元格定义都有其行和列的键,以及一个字符串,该字符串应显示在矩阵中 public class CellViewModel : ViewModelBase { public object RowKey { get; set; } public object ColumnKey { get; set; } public s
ObserverableCollection
中收集。然后我将按单元格定义填充集合。每个单元格定义都有其行和列的键,以及一个字符串,该字符串应显示在矩阵中
public class CellViewModel : ViewModelBase
{
public object RowKey { get; set; }
public object ColumnKey { get; set; }
public string CellText { get; set; }
}
这意味着,当一个CellViewModel
被添加到集合中,并且已经存在具有相同RowKey的行时,该单元格将填充到此行的适当列中。如果该列不存在,将添加一个新列。关键点可以是任何对象,有了它,人可以用任何对象关键点添加一个单元格,而不需要关心设置单元格的正确位置。例如,我用CellViewModels
填充集合:
GridCellViewModelCollection = new ObservableCollection<CellViewModel>
{
new CellViewModel
{
RowKey = 1,
ColumnKey = 1,
CellText = "Cell1_1"
},
new CellViewModel
{
RowKey = "string",
ColumnKey = 'char',
CellText = "Cell2_2"
},
new CellViewModel
{
RowKey = 3,
ColumnKey = 1,
CellText = "Cell3_1"
},
new CellViewModel
{
RowKey = 1,
ColumnKey = 3,
CellText = "Cell1_3"
},
new CellViewModel
{
RowKey = 3,
ColumnKey = 'char',
CellText = "Cell3_2"
}
}
我已经尝试使用ItemsControl并将整个集合转换为适合使用PlaceHolder的ItemsControl
var list = new List<CellViewModel>{ null, null, cellViewModel };
var list=新列表{null,null,cellViewModel};
但我认为这不是一个好主意。而且占位符持有者不会保留将整个矩阵保持在良好状态的位置。此外,ItemsControl没有行和列的标题
有什么想法或建议吗?我应该使用哪个控件来执行此操作?您可以在ItemsControl中使用网格作为ItemsPanel,并在
ItemContainerStyle
中以样式绑定Grid.Column
和Grid.Row
属性。当然,列和行索引是以零为基础的
<ItemsControl ItemsSource="{Binding GridCellViewModelCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid IsItemsHost="True">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding ColumnKey}"/>
<Setter Property="Grid.Row" Value="{Binding RowKey}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding CellText}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
克莱门斯:我不知道在ItsSt控件中使用网格作为ItStupEL。我已经尝试了Grid withoud Items控件和GridView。谢谢!但已使用对象定义了ColumnKey和RowKey。将用于添加任何对象作为关键点的新ViewModel。这不适用于Grid.Column或Grid.Row。我仍然不知道如何正确显示行和列的标题,除了添加虚拟单元格……您可以使用绑定转换器从RowKey和ColumnKey中的对象获取整数行和列索引。可以在外部添加标题,例如,将ItemsControl放在外部网格中。我尝试了另一种方法在ItemsControl中显示数据。标题总是有问题的。如果我对单元格使用DataTemplate,则不会保持相同的级别。这似乎不容易解决。如何将标题的宽度与其列中的最大宽度绑定?我曾试过那样做,但没有成功。这是xaml:Width=“{Binding CellWidth,Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged}”中的代码。我将CellWidth定义为double,但只得到NaN。
<ItemsControl ItemsSource="{Binding GridCellViewModelCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid IsItemsHost="True">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding ColumnKey}"/>
<Setter Property="Grid.Row" Value="{Binding RowKey}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding CellText}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>