c# - 具有行和列标题WPF的ItemsControl

标签 c# wpf mvvm binding

我需要将对象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/

相关文章:

c# - 包含可变引用类型成员的值类型是否被视为不可变?

c# - 帮助理解 Enumerable.Join 方法

.net - MVVM 设计考虑

c# - 使用以编程方式创建的设置任务栏覆盖图标

c# - 如何为数据绑定(bind)启用属性更改?

c# - Web 服务方法 - 无法序列化,因为它没有无参数构造函数

c# - 通过 MSCOMM 将 VB6 转换为 C# 到串行端口

wpf - 使用 GeometryDrawing WPF 创建按钮

wpf - 变更跟踪和并发 - 这会失败吗?

c# - 获取列表框中复选框的 IsChecked 属性