views:

1676

answers:

2

Hello,

I am wrestling with a binding problem in WPF/Silverlight. I have a Listview witch is filled by a DataContext form an EF linq query. In the same usercontrol are textboxes. When changing their values, the listview gets refresht and the data is changed in de db bij .SaveChanges. The problem is that if I use a combobox the data is saved but de listview isn't updated.

Can you be of help???? Here is the xaml

     <ListView Grid.Row="1" Grid.Column="0"  Margin="4,4,4,0" x:Name="controlsListBox" Grid.RowSpan="7" ItemsSource="{Binding}" SelectedValuePath="ID" LostFocus="controlsListBox_LostFocus">
        <ListView.View>
           <GridView>
              <GridViewColumn Width="25" Header="Rw" DisplayMemberBinding="{Binding RowNr}"/>
              <GridViewColumn Width="25" Header="Cl" DisplayMemberBinding="{Binding ColumnNr}"/>
              <GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}"/>
              <GridViewColumn Width="25" Header="Tb" DisplayMemberBinding="{Binding TabIndex}"/>
              <GridViewColumn Width="100" Header="Type" DisplayMemberBinding="{Binding ControlTypes.Name}"/>
              <GridViewColumn Width="100" Header="Text" DisplayMemberBinding="{Binding TextResources.Text}"/>
           </GridView>
        </ListView.View>
     </ListView>

     <Label Grid.Row="2" Grid.Column="5" Height="23" Margin="4,4,4,0" x:Name="rowSpanLabel" VerticalAlignment="Top"
            Content="RowNr"/>
     <TextBox Grid.Row="2" Grid.Column="6" Height="23" Margin="4,4,4,0" x:Name="rowSpanTextBox" VerticalAlignment="Top" 
              Text="{Binding Path=SelectedItem.RowNr, ElementName=controlsListBox}"/>

     <Label Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Height="23" Margin="4,4,4,0" x:Name="controlTypeLabel" VerticalAlignment="Top"
            Content="Type"/>
     <ComboBox Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="5" Height="23" Margin="4,4,4,0" x:Name="controlTypeComboBox" VerticalAlignment="Top"
               DataContext="{Binding  Path=ControlTypes, ElementName=controlsListBox}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name"
               SelectedItem="{Binding Path=SelectedItem.ControlTypes, ElementName=controlsListBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
               />

And here the C# code: _controlProperties.Clear(); var data = (from x in _dataContext.ControlProperties where x.FormProperties.ID == 1 orderby x.RowNr, x.ColumnNr, x.Name select x); foreach (var item in data) { item.TextResourcesReference.Load(); _controlProperties.Add(item); } // DataContext must first be set to null for good result. controlsListBox.DataContext = null; controlsListBox.DataContext = _controlProperties;

     controlTypeComboBox.DataContext = (from c in _dataContext.ControlTypes
                                        orderby c.Name
                                        select c).ToList();
A: 

You are setting the DataContext of the ComboBox, but not the ItemsSource. In code, you are overwriting that DataContext by providing a list of control types, so that part of the XAML is ignored anyway.

Remove the DataContext declaration and use this instead:

ItemsSource="{Binding}"

That should cause the control types to appear in the combo box. When I do this, the selected control type gets displayed in the list view.

You might also want to look into Update Controls .NET, my open-source alternative to WPF data binding. It takes some of the bookkeeping out of the bound classes.

Michael L Perry
A: 

Thanks for the answer. I have just changed the code to use business layer classes with observable collections. Everything works fine now. I am still wondering if a direct binding to linq queries will be working good.