简体   繁体   English

MVVM将DataGrid绑定到DataTable显示错误的列名

[英]MVVM Binding DataGrid to DataTable shows wrong Column Name

I am new to C# WPF, Anyone have come across the case before? 我是C#WPF的新手,以前有人遇到过这个案例吗? (MVVM) (MVVM)

I bind the ItemSource of a DataGrid to a DataTable. 我将DataGrid的ItemSource绑定到DataTable。 When I use SQL command "Select * from Testing1" (its connecting to a MDB file), all the data are corrected populated in the DataGrid, except the column Name has some issue. 当我使用SQL命令“从Testing1中选择*”(它连接到MDB文件)时,所有数据均已纠正,并已填充到DataGrid中,但“名称”列存在问题。 "Underscore" was missing strangely in the column name. 列名称中奇怪地缺少“ Underscore”。 I tested below case: 我测试了以下情况:

  • The correct Column Name is "Testing_Field_3", while it display "TestingField_3". 正确的列名称为“ Testing_Field_3”,同时显示“ TestingField_3”。
  • The correct Column Name is "Testing_ Field_ _4", while it display "Testing_Field_4". 正确的列名称为“ Testing_ Field_ _4”,同时显示“ Testing_Field_4”。 (Displaying underscore in this website have some issue. The case should be that 2 underscore converted to 1 underscore) (在该网站上显示下划线存在一些问题。这种情况应该是将2个下划线转换为1个下划线)

When debugging, I checked the DataTable/Recordset, and the column name is correct and no issue. 调试时,我检查了DataTable / Recordset,列名正确并且没有问题。

Below is the extracted part of my xaml: 以下是我的xaml的摘录部分:

<DataGrid Grid.Column="0" Grid.Row="1" Width="650" Height="300" Margin="5,5,0,0" AutoGenerateColumns="True" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding Path=SqlDataTbl, Mode=OneWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" />

If it was a bug in C#, any alternative for this? 如果这是C#中的错误,那么还有其他选择吗?

Kindly appreciate help from you guys and thanks in advance. 谢谢您的帮助,并在此先感谢。

Reason for this behaviour is ContentPresenter.RecognizesAccessKey which is by default true for datagrid header contentPresenter . 此行为的原因是ContentPresenter.RecognizesAccessKey ,对于datagrid header contentPresenter默认为true If this property is true, it uses AccessText in text of header. 如果此属性为true,则在标题文本中使用AccessText

Workaround for this is to hook to AutoGeneratingColumn event of dataGrid and replace all single underscore with double underscores. 解决方法是挂接到dataGrid的AutoGeneratingColumn事件,然后用双下划线替换所有单下划线。 Sample is shown below - 示例如下所示-

<DataGrid AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"/> 

and in event handler - 并在事件处理程序中-

private void DataGrid_AutoGeneratingColumn(object sender, 
                                    DataGridAutoGeneratingColumnEventArgs e)
{
   e.Column.Header = e.Column.Header.ToString().Replace("_", "__");
}

Also you can achieve that by overriding ContentTemplate of DataGridColumnHeader . 您也可以通过overriding ContentTemplate DataGridColumnHeader overriding ContentTemplate来实现。 Use TextBlock inside it which does not recognizes access key from a text. 在其中使用不能识别文本访问键的TextBlock

<DataGrid ItemsSource="{Binding Objects}">
  <DataGrid.ColumnHeaderStyle>
     <Style TargetType="{x:Type DataGridColumnHeader}"
            BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{TemplateBinding Content}" 
                               HorizontalAlignment="Center"/>
                 </DataTemplate>
            </Setter.Value>
        </Setter>
     </Style>
  </DataGrid.ColumnHeaderStyle>
</DataGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM