0

I have a DataGrid which displays a list of objects. One of the properties in the objects is another custom object. This object is shown as a ComboBox in the grid. When I change the selected item in the ComboBox from the grid, everything seems to work as I expect. However, when I change the selected item from code behind, the SelectedItem in the ComboBox doesn't update. I have implemented the INotifyPropertyChanged and the event is firing as it should. I have also tried to print the domain name in a TextBox and it shows the correct value. The problem I have is that the SelectedItemBinding doesn't seem to work when I update from code behind. Can anyone explain why? Here is my code:

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>

My objects

 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));
        }
    }
}

Init code

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

Update code

foreach (Entity entity in Entities)
            {
                entity.Domain = getDefaultDomain();
            }
Andy
  • 30,088
  • 6
  • 78
  • 89
Abris
  • 1,451
  • 3
  • 18
  • 38
  • Where is the `Entities` in your update code coming from? Is it the same instance that you set as the `ItemsSource` in your init code? – Andy Mar 21 '14 at 18:50
  • Thanks for your comment. You were at the right path mentioning instances. The problem was that I created a a new Domain Instance, my guess is that it made the new object inequal to the objects in the combobox itemsource. – Abris Mar 21 '14 at 19:19

2 Answers2

1

Solved the problem after a couple hours of head ache. In my update code, I created a new instance of Domain. My guess is that it made it inequal to the objects in the current itemsource. My solution to the problem was to select the Domain from the original Domain ItemSource and thereafter assign it to the Entity.

Abris
  • 1,451
  • 3
  • 18
  • 38
0

try this:

  • change the list to ObservableCollection

    List<Domain> to ObservableCollection<Domain>
    
  • change the combobox to

    <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>
    

cause the Domain object have many properties you should set the selected value to the prmary key of the Domain object and the displaymemberpath to the value that you want to show - i think in you case the Domain Name.

Henka Programmer
  • 433
  • 6
  • 18