C# 带有组合框的WPF ListView
我们有带ComboBox的ListView。我正在通过单击按钮将数据绑定到listView。我想显示带有选定值的组合框。当我们设置IsSynchronizedWithCurrentItem=“True”时,它将更新combobox的所有值,但当我们设置IsSynchronizedWithCurrentItem=“False”时,combobox将不显示所选值。为什么?我使用MVVM模型来绑定列表视图和组合框的数据,而不是硬编码。如果您有解决方案,请与我们分享C# 带有组合框的WPF ListView,c#,wpf,C#,Wpf,我们有带ComboBox的ListView。我正在通过单击按钮将数据绑定到listView。我想显示带有选定值的组合框。当我们设置IsSynchronizedWithCurrentItem=“True”时,它将更新combobox的所有值,但当我们设置IsSynchronizedWithCurrentItem=“False”时,combobox将不显示所选值。为什么?我使用MVVM模型来绑定列表视图和组合框的数据,而不是硬编码。如果您有解决方案,请与我们分享 <GridViewColumn
<GridViewColumn Header="Part ID">
<GridViewColumn.CellTemplate>
<DataTemplate>
<telerik:RadComboBox x:Name="PartIdListViewRadComboBox"
CanAutocompleteSelectItems="True" IsEditable="True"
Width="105" Height="30" Margin="4,4,4,4"
VerticalAlignment="Center" StaysOpenOnEdit="False"
IsSynchronizedWithCurrentItem="False"
SelectedValue="{Binding Path=ProductID}"
DisplayMemberPath="PartID" EmptyText="Part ID"
HorizontalAlignment="Left"
ItemsSource="{ Binding RelativeSource=
{RelativeSource FindAncestor,AncestorType={x:Type ListView}},
Path=DataContext.ProductModel.ListViewComboboxProductListCollection}"
OpenDropDownOnFocus="True"
TextSearchMode="Contains" IsFilteringEnabled="True"
IsTextSearchEnabled="True">
<!--
<ie:Interaction.Triggers>
<ie:EventTrigger EventName="SelectionChanged">
-->
<!--
<si:CallMethodAction MethodName="SelectionChanged"
TargetObject="{Binding}" /> -->
<!--
<ie:InvokeCommandAction Command="{Binding RelativeSource=
{RelativeSource FindAncestor,AncestorType={x:Type ListView}},
Path=DataContext.PartIdComboboxChangedComand}"
CommandParameter="{Binding ElementName=PartIdListViewRadComboBox}"/>
</ie:EventTrigger>
</ie:Interaction.Triggers>-->
</telerik:RadComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
我的最后一个想法是使用
项目模板
:
<telerik:RadComboBox ... >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=PartID}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
...
</telerik:RadComboBox>
...
我的最后一个想法是使用项目模板
:
<telerik:RadComboBox ... >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=PartID}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
...
</telerik:RadComboBox>
...
我不确定这是否是您实际代码中的内容,或者这只是您问题中的一个错误,但您使用的是IsSynchronizedWithCurrentItem=“Fa”
而不是IsSynchronizedWithCurrentItem=“False”
。IsSynchronizedWithCurrentItem=“Fa”。这是错误的,我不在乎。请阅读IsSynchronizedWithCurrentItem=“False”如果任何主体了解MVVM的WPF,请分享您的观点,不要浪费时间只是为了澄清,当您说“然后组合框不显示选定值”时,这是否意味着组合框没有任何可供选择的项目?或者,这是否意味着组合框下拉列表中有值,但所选值为空?是的,所选值为空。我不确定这是您实际代码中的值还是您的问题中的错误,但您使用的是IsSynchronizedWithCurrentItem=“Fa”
而不是IsSynchronizedWithCurrentItem=“False”
.IsSynchronizedWithCurrentItem=“Fa”。这是错误的,我不在乎。请阅读IsSynchronizedWithCurrentItem=“False”如果任何主体了解MVVM的WPF,请分享您的观点,不要浪费时间只是为了澄清,当您说“然后组合框不显示选定值”时,这是否意味着组合框没有任何可供选择的项目?或者这是否意味着组合框下拉列表中有值,但所选值为空?是,所选值为ListView中的emptycombobox它产生错误“其他信息:在使用ItemsSource之前Items集合必须为空。”ListView中的combobox它产生错误“其他信息:在使用ItemsSource之前Items集合必须为空。”