Refresh data binding issues between Listview and ComboBox

I am struggling with a binding issue in WPF / Silverlight. I have a Listview witch filled with a DataContext form with a linq EF request. In the same usercontrol there are text fields. When their values ​​change, the listview gets a refresht and the data changes in debbj.SaveChanges. The problem is, if I use the combobox, the data persists, but the list is not updated.

You can 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 is the C # code: _controlProperties.Clear (); var data = (from x to _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 be null for a valid result. controlsListBox.DataContext = null; controlsListBox.DataContext = _controlProperties;

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

      

+1


source to share


2 answers


You are setting the DataContext for the ComboBox, but not the ItemsSource. In your code, you rewrite the DataContext to provide a list of control types, so the XAML portion is ignored anyway.

Remove the DataContext declaration and use this instead:

ItemsSource="{Binding}"

      



This should cause the control types to appear in the combo box. When I do this, the selected control type is displayed as a list.

You can also have a look at Update Controls.NET , my open source alternative for WPF data binding. It takes some of the bookkeeping from related classes.

0


source


Thanks for the answer. I just changed the code to use business layer classes with observable collections. Everything works fine now. I'm still wondering if direct binding to linq queries will work.



0


source







All Articles