1

我正在努力解决 WPF/Silverlight 中的绑定问题。我有一个 Listview 女巫,由 EF linq 查询的 DataContext 填充。在同一个用户控件中是文本框。更改它们的值时,列表视图会刷新,并且数据会在 de db bij .SaveChanges 中更改。问题是,如果我使用组合框,则会保存数据,但不会更新列表视图。

你能帮上忙吗???这是xml

     <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}" 
               />

这里是 C# 代码:_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(项目); } // DataContext 必须首先设置为 null 以获得良好的结果。控件列表框.DataContext = null; 控件列表框.DataContext = _controlProperties;

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

2 回答 2

0

您正在设置 ComboBox 的 DataContext,而不是 ItemsSource。在代码中,您通过提供控件类型列表来覆盖该 DataContext,因此无论如何都会忽略 XAML 的一部分。

删除 DataContext 声明并改用它:

ItemsSource="{Binding}"

这应该会导致控件类型出现在组合框中。当我这样做时,选定的控件类型会显示在列表视图中。

您可能还想查看Update Controls .NET,这是我的 WPF 数据绑定的开源替代方案。它需要一些簿记超出绑定类。

于 2009-01-09T16:21:03.603 回答
0

感谢你的回答。我刚刚更改了代码以使用具有可观察集合的业务层类。现在一切正常。我仍然想知道直接绑定到 linq 查询是否会正常工作。

于 2009-01-23T18:54:56.317 回答