我正在与WPF / Silverlight中的绑定问题搏斗。我有一个Listview女巫由DataContext表单填充EF linq查询。在同一个usercontrol中是文本框。更改其值时,listview将获取刷新,并且数据将在de db bij .SaveChanges中更改。问题是,如果我使用组合框,则会保存数据,但不会更新de listview。
你可以帮忙吗???? 这是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}"
/>
这里是C#代码: _controlProperties.Clear(); var data =(来自_dataContext.ControlProperties中的x 其中x.FormProperties.ID == 1 orderby x.RowNr,x.ColumnNr,x.Name 选择x); foreach(数据中的var项) { item.TextResourcesReference.Load(); _controlProperties.Add(项目); } //首先必须将DataContext设置为null才能获得良好的结果。 controlsListBox.DataContext = null; controlsListBox.DataContext = _controlProperties;
controlTypeComboBox.DataContext = (from c in _dataContext.ControlTypes
orderby c.Name
select c).ToList();
答案 0 :(得分:0)
您正在设置ComboBox的DataContext,但不设置ItemsSource。在代码中,您通过提供控件类型列表来覆盖DataContext,因此无论如何都会忽略部分XAML。
删除DataContext声明并改为使用:
ItemsSource="{Binding}"
这应该导致控件类型出现在组合框中。执行此操作时,所选控件类型将显示在列表视图中。
您可能还想查看Update Controls .NET,这是我对WPF数据绑定的开源替代方案。它需要一些簿记课程中的簿记。
答案 1 :(得分:0)
感谢您的回答。我刚刚更改了代码,以使用具有可观察集合的业务层类。现在一切都很好。 我仍然想知道对linq查询的直接绑定是否会正常工作。