数据网格中的ComboBox未更新

时间:2014-03-21 17:05:00

标签: c# wpf datagrid combobox

我有一个显示对象列表的DataGrid。对象中的一个属性是另一个自定义对象。此对象在网格中显示为ComboBox。当我从网格中更改ComboBox中的所选项目时,一切似乎都按预期工作。但是,当我从后面的代码更改所选项目时,SelectedItem中的ComboBox不会更新。我已经实现了INotifyPropertyChanged并且事件正在按照它应该发射。我还尝试在TextBox中打印域名,并显示正确的值。我遇到的问题是,当我从后面的代码更新时,SelectedItemBinding似乎不起作用。有谁能解释为什么?这是我的代码:

XAML

    <DataGrid AutoGenerateColumns="False" Height="506" HorizontalAlignment="Left" Name="EntityGrid" VerticalAlignment="Top" Width="360" Margin="10,62,0,0">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}">
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
                <EventSetter Event="PreviewTextInput" Handler="DataGridCell_PreviewTextInput" />
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding dbName, Mode=TwoWay}" />
            <DataGridComboBoxColumn x:Name="Domain" Header="Domain"  SelectedItemBinding="{Binding Domain, Mode=TwoWay}" DisplayMemberPath="DomainName}"/>
            <DataGridCheckBoxColumn Header="Static" Binding="{Binding Static}" IsReadOnly="True" />
        </DataGrid.Columns>
    </DataGrid>

我的对象

 public class Entity : INotifyPropertyChanged
{
    private string _dbName;

    public string dbName { get { return _dbName; } set { _dbName = value; NotifyPropertyChanged("dbName"); } }

    public string EntityName { get; set; }

    private Domain _domain;
    public Domain Domain
    {
        get { return _domain; }
        set
        {
            _domain = value;
            NotifyPropertyChanged("Domain");
        }
    }

    public bool Static { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class Domain : INotifyPropertyChanged
{
    public string DomainName { get; set; }

    public string ContextName { get; set; }

    public string FileName { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

初始代码

            List<Domain> domains = getDomainsFromConfig();
        List<Entity> Entities = dal.getAllEntities(config.Paths.dbml.AllEntities, config.Paths.dbml.LockedEntities);
        EntityGrid.ItemsSource = Entities;
        Domain.ItemsSource = domains;

更新代码

foreach (Entity entity in Entities)
            {
                entity.Domain = getDefaultDomain();
            }

2 个答案:

答案 0 :(得分:1)

头痛几个小时后解决了这个问题。在我的更新代码中,我创建了一个新的Domain实例。我的猜测是它使它不等于当前itemsource中的对象。我对此问题的解决方案是从原始Domain ItemSource中选择域,然后将其分配给实体。

答案 1 :(得分:0)

试试这个:

  • 将列表更改为ObservableCollection

    List<Domain> to ObservableCollection<Domain>
    
  • 将组合框更改为

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="domain">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox SelectedValue="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    

因为Domain对象有很多属性,你应该将所选值设置为Domain对象的prmary键,将displaymemberpath设置为你想要显示的值 - 我认为在你的情况下是域名。

相关问题