所选项目未显示在DataGridComboBoxColumn中 [英] Selected item not displayed in the DataGridComboBoxColumn

查看:109
本文介绍了所选项目未显示在DataGridComboBoxColumn中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGrid ,并且在XAML中设置了两个 DataGridComboBoxColumn (复制自此处):

I have a DataGrid with two DataGridComboBoxColumns set in the XAML (copied from here):

<DataGrid x:Name="joinGrid" AutoGenerateColumns="False" CanUserAddRows="True">
                <DataGrid.Columns>
                    <DataGridComboBoxColumn>
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="IsDropDownOpen" Value="True" />
                                <Setter Property="ItemsSource" Value="{Binding Path=TableAColumns}" />
                                <Setter Property="ItemTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding}"></TextBlock>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle >
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Path=TableAColumns}" />
                                <Setter Property="IsDropDownOpen" Value="True" />
                                <Setter Property="ItemTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding}"></TextBlock>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>

                    <DataGridComboBoxColumn>
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="IsDropDownOpen" Value="True" />
                                <Setter Property="ItemsSource" Value="{Binding Path=TableBColumns}" />
                                <Setter Property="ItemTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding}"></TextBlock>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Path=TableBColumns}" />
                                <Setter Property="IsDropDownOpen" Value="True" />
                                <Setter Property="ItemTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding}"></TextBlock>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>
                </DataGrid.Columns>
            </DataGrid>

几乎没有例外,因为老实说我不明白丢失的小东西应该走些什么。我试图将这些列绑定到实体类中包含的字符串列表:

with few exceptions because honestly i do not understand what should go in the missing porperties. I am trying to bind the columns to lists of strings contained in an entity class:

class JoinBlockDTO
{
    public List<string> TableAColumns { get; set; }
    public List<string> TableBColumns { get; set; }
}

在我的代码后面,我设置了网格的 ItemsSource

In my code behind, i set the grid's ItemsSource:

DataGrid dg = (DataGrid)GetTemplateChild("joinGrid");

List<JoinBlockDTO> l = new List<JoinBlockDTO>();
List<string> colAList = (from DataRowView row in sourceA
                         select row.Row.Field<string>(0)).ToList();
List<string> colBList = (from DataRowView row in sourceB
                         select row.Row.Field<string>(0)).ToList();

((DataGridComboBoxColumn)dg.Columns[0]).ItemsSource = colAList; //doesn't work without this
((DataGridComboBoxColumn)dg.Columns[1]).ItemsSource = colBList; //doesn't work without this
l.Add (new JoinBlockDTO(){TableAColumns = colAList, TableBColumns = colBList});
dg.ItemsSource = l;

所有这些最初都会渲染一个2 x 2的网格,其中包含所有空白单元格。我单击两次以查看一个下拉列表,其中包含正确绑定的字符串列表。但是,当我从下拉列表中选择一个值并将焦点移到外部时,该单元格将保持空白。有人可以指出应添加到XAML或对其进行修改的内容吗?有没有更简单的方法来执行此绑定?我已经阅读了许多有关此组合框列绑定的文章,但无法理解发生了什么。 (使用VS2010)

All this, initially, renders a 2 x 2 grid with all blank cells. I click twice to see a drop-down with the list of strings bound properly. However, when i select a value from the drop-down and shift the focus outside, the cell is left blank. Can someone point out what should be added to the XAML or modified? Is there a simpler way to do this binding? I have read numerous posts about this combo-box column binding but could not understand what was going on. (Using VS2010)

推荐答案

这将帮助您

使用 ObservableCollection自动INotifyPropertyChanged的列表

use "ObservableCollection"List to auto INotifyPropertyChanged

 <DataGrid x:Name="myDataGridTeacher" 
                  ItemsSource="{Binding ObservableCollectionList}" >

 <DataGridTemplateColumn Width="*" Header="MyHeader"  >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate >

                            <TextBlock Text="{Binding Material_Name.Name}" 
                                  />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox  
                                       ItemsSource="{Binding Material_Name}" 
                                       DisplayMemberPath="Name"

                                />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>

                </DataGridTemplateColumn>

 </DataGrid>

ItemsSource = {Binding Material_Name}是 ObservableCollectionList中的列表

ItemsSource="{Binding Material_Name}" is List in "ObservableCollectionList"

<TextBlock Text="{Binding Material_Name.Name}" 
                                      />

这篇关于所选项目未显示在DataGridComboBoxColumn中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆