所选项目未显示在DataGridComboBoxColumn中

时间:2012-08-20 15:03:37

标签: .net wpf c#-4.0 datagrid datagridcomboboxcolumn

我有一个DataGrid在XAML中设置了两个DataGridComboBoxColumn(从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>

除了少数例外,因为说实话,我不明白丢失的属性应该是什么。我试图将列绑定到实体类中包含的字符串列表:

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

在我的代码中,我设置了网格的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)

3 个答案:

答案 0 :(得分:0)

也许您的VM没有实现INotifyPropertyChanged接口。

以下是一个例子:

<Window x:Class="wpfProj.MainWindow"
        ...
        d:DataContext="{d:DesignInstance wpfProj:MainViewModel}">

    <Grid >
        <Grid.Resources>
            <wpfProj:BindingProxy x:Key="proxy" Data="{Binding}"/>
        </Grid.Resources>
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Persons}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="*"/>
                <DataGridComboBoxColumn Header="Sex" Width="*" ItemsSource="{Binding Source={StaticResource proxy}, Path=Data.SexList}"
                                        SelectedItemBinding="{Binding Path=Sex}"/>
                <DataGridComboBoxColumn Header="Sex" Width="*" ItemsSource="{Binding Source={x:Static wpfProj:DataCollection.Instance}, Path=SexList}"
                                        SelectedItemBinding="{Binding Path=Sex}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

我放置了两个“性别”列,只是为了显示绑定到源的两种方法。在第一种情况下,SexList是ViewModel(DataContext)的一部分,这就是我使用代理的原因。在第二种情况下,它与单身人士的财产绑定。

性爱属性:

    public string Sex
    {
        get { return _sex; }
        set
        {
            _sex = value;
            OnPropertyChanged("Sex");
        }
    }

代理类(我在互联网上发现它,不记得作者页面的链接)

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}

希望它有所帮助。 以下是我上传的sample

答案 1 :(得分:0)

是的,我明白了。

使用SelectedValueBinding = {Binding object}和SelectedValuePath =“” 这为我解决了。

WPF: Databinding with DataGridComboBoxColumn

答案 2 :(得分:0)

这将有助于你

使用&#34; ObservableCollection&#34;列表自动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 =&#34; {Binding Material_Name}&#34;是&#34; ObservableCollectionList&#34;

中的列表
<TextBlock Text="{Binding Material_Name.Name}" 
                                      />