我需要将对象ObservableCollection<RegisterBean> listOfRegisters
的可更新列表RegisterBean
表示为具有行和列标题的矩阵对象包含3个属性:
byte deviceAddress;
byte register;
byte[] data;
并且仅应显示一个属性数据。预期结果如图片所示:
我使用通常的datagrid达到了此结构,但问题是要对其进行更新,因为我使用了
new DataTable()
作为返回值的转换器。这是不正确的,因为它会闪烁并重新渲染所有对象。昨天我要求帮助here。这个解决方案正在工作:
<ItemsControl ItemsSource="{Binding listOfRegisters}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="16"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Data, Converter={StaticResource ByteToStringValueConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
但是我不明白如何更改用户控件的结构和添加标题。使用itemscontrol
的解决方案的结果:谢谢!
UPD:
header 应该是静态的,没有任何排序和类似的东西
最佳答案
我开始说可能是,检查DataGrid出了什么问题会更好,并且不必重写控件,但是如果您仍然想使用ItemsControl
...
假设ViewModel中有两个包含Header的集合(ColHeaders和RowHeaders),则可以添加另外两个保存标题的面板。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<!-- Column Headers -->
<ItemsControl Grid.Column="1" Grid.Row="0" ItemsSource="{Binding ColHeaders}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding}" Width="30" Height="30"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl Grid.Row="1" Grid.Column="1" ItemsSource="{Binding listOfRegisters}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="16"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Data, Converter={StaticResource ByteToStringValueConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- RowHeaders -->
<ItemsControl Grid.Column="0" Grid.Row="1" ItemsSource="{Binding RowHeaders}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding}" Width="30" Height="30"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
我还调整了TextBlocks和TextBoxes的大小,以对齐所有内容。
关于c# - 具有行和列标题WPF的ItemsControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51396486/