Wpf MVVM组合框listview中的选定项绑定
我正在尝试将组合框绑定到列表视图中。 以下是Xaml:Wpf MVVM组合框listview中的选定项绑定,wpf,xaml,listview,mvvm,combobox,Wpf,Xaml,Listview,Mvvm,Combobox,我正在尝试将组合框绑定到列表视图中。 以下是Xaml: <UserControl <UserControl.Resources> <Style TargetType="ListViewItem"> <Style.Triggers> <Trigger Property="IsKeyboardFocusWithin" Value="true">
<UserControl
<UserControl.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter Property="IsSelected" Value="true" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<DockPanel MinWidth="724" Height="387" DataContext="{Binding DeviceDiagnosticsMainVeiwModel}">
<globals:SaveNotificationPopup IsOpen="{Binding IsSaveNotificationPopupOpen}" SaveCommand="{Binding SaveCommand}" CancelCommand="{Binding CloseSaveNotificationPopup}" Placement="Relative" VerticalOffset="300" HorizontalOffset="200" />
<StackPanel>
<Grid Width="250" HorizontalAlignment="Left" Margin="10,10,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="23" />
<RowDefinition Height="Auto" MinHeight="23" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Content="{x:Static ml:MultiLang._2001}" x:Name="ML_0209" />
<Label Grid.Row="1" Content="{x:Static ml:MultiLang._2009}" x:Name="ML_0211" />
<ComboBox Grid.Column="1" Grid.Row="0" ItemsSource="{Binding Manufacturers}" DisplayMemberPath="WorkingPlace" SelectedValuePath="PersonCode" SelectedValue="{Binding Path=ManufacturerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Devices}" DisplayMemberPath="Model" SelectedValue="{Binding Path=SelectedDevice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
<ListView DockPanel.Dock="Top" Margin="10,25,10,10" Width="460" ScrollViewer.HorizontalScrollBarVisibility="Hidden" HorizontalAlignment="Left" ItemsSource="{Binding CurrentDeviceDiagnostics}" SelectedItem="{Binding SelectedDeviceDiagnostics}" BorderThickness="1">
<ListView.View>
<GridView>
<GridViewColumn Header="Command" Width="180" x:Name="ML_0007">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding DiagnosticsCommand, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" FontSize="12" Margin="0 5 0 0" Width="165" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" Width="180" x:Name="ML_0010">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding DiagnosticsName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" FontSize="12" Margin="0 5 0 0" Width="165" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Units" Width="100" x:Name="ML_0013">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Units}" DisplayMemberPath="UnitName" SelectedValuePath="UnitCode" SelectedValue="{Binding DiagnosticsUnit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="85" Margin="0,1,0,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ContentControl DockPanel.Dock="Top" Content="{Binding AddNewCAView}" />
<!-- No Diagnodtics message-->
<globals:NoItemsMessage DockPanel.Dock="Top" Visibility="{Binding NoDiagnosticsVisibility}" HorizontalAlignment="Left" Margin="10" Width="360" Height="40" Message="{x:Static ml:MultiLang._742}" x:Name="ML_0017" />
<!-- delete popup -->
<globals:DeletePopup IsOpen="{Binding IsConfirmDeletePopupOpen}" Message1="{x:Static ml:MultiLang._746}" DeleteCommand="{Binding DeleteCommand}" CancelCommand="{Binding CancelDeleteCommand}" Placement="Center" VerticalOffset="-200" HorizontalOffset="200" x:Name="ML_0018" />
</StackPanel>
</DockPanel>
</UserControl>
并且CurrentDeviceDiagnostics和SelectedDeviceDiagnostics都是DeviceDiagnostics维护模型的成员
列表视图中的一个文本框(例如)如下绑定:
Text="{Binding DiagnosticsCommand, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
CurrentDeviceDiagnostics是DeviceDiagnosticsVeiwModel的可观察集合,其中包含DiagnosticsCommand
而且效果很好(两个文本框)
复选框绑定如下:
ItemsSource="{Binding Units}" DisplayMemberPath="UnitName" SelectedValuePath="UnitCode" SelectedValue="{Binding DiagnosticsUnit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
而Units和DiagnosticsUnit也是DeviceDiagnosticsVeiwModel的成员
项目源工作正常,并按我需要的方式填充。
所选项目存在问题。。。虽然DiagnosticsUnit具有整数值,但其初始化为空
有趣的是,如果我从组合框中选择某个内容,代码将转到集合函数的DiagnosticsUnit属性,因此我不确定这个问题。。。
即使我将使用DiagnosticsUnit将组合框更改为文本框,它也会工作
我使用了很多mvvm组合框,没有任何绑定问题,但这是我第一次在列表视图中使用组合框
请帮帮我
谢谢大家 让我们来分析一下。。。你说:
CurrentDeviceDiagnostics
和SelectedDeviceDiagnostics
都是DeviceDiagnosticsMainViewModel
的成员
这是应该的。然后你说:
CurrentDeviceDiagnostics
是一个ObservableCollection
因此,CellTemplate
s内部的数据绑定应该指向您正确指出的DeviceDiagnosticsViewModel
类中的属性
因此,看看下面的组合框
代码,我只能想到两个可能的原因,为什么这不起作用:
<ComboBox ItemsSource="{Binding Units}" DisplayMemberPath="UnitName"
SelectedValuePath="UnitCode" SelectedValue="{Binding DiagnosticsUnit, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="85" Margin="0,1,0,0" />
一个建议是使用组合框。改为选择editem
属性,并将数据绑定到与单位
集合相同类型的实例。。。但是,如果这只是int
s的集合,那么这一步就没有帮助了。唯一的另一件事是确保您在DeviceDiagnosticsViewModel
类中正确实现了INotifyPropertyChanged
接口。
我终于发现了问题!
这不是绑定问题。
这太傻了。
我有这个密码
public void GetItems()
{
_DeviceDiagnostics=新的可观察采集(
(来自于dd in_dal.GetItems()
orderby dd.DeviceID
选择新设备DiagnosticsVeiwmodel(dd)
{
ObjectStatus=Status.NoChange,
Units=\u manager.UnitRepository.Units
}
).ToObservableCollection()
);
}
这些单位是视图模型(DeviceDiagnosticsVeiwModel)中的一个成员,实际上是comboboxs项源。
在DeviceDiagnosticsVeiwModel内部有一个int属性,它是所选的项。它装得不对
将显示正确的代码
public void GetItems()
{
_devicesDiagnostics = new ObservableCollection<DeviceDiagnosticsVeiwModel>(
(from dd in _dal.GetItems()
orderby dd.DeviceID
select new DeviceDiagnosticsVeiwModel(dd)
{
ObjectStatus = Status.NoChange,
Units = new ObservableCollection<UnitViewModel>(this.Units),
}
)
.ToObservableCollection<DeviceDiagnosticsVeiwModel>()
);
}
public void GetItems()
{
_DeviceDiagnostics=新的可观察采集(
(来自于dd in_dal.GetItems()
orderby dd.DeviceID
选择新设备DiagnosticsVeiwmodel(dd)
{
ObjectStatus=Status.NoChange,
单位=新的可观测集合(此.Units),
}
)
.ToObservableCollection()
);
}
我必须给每个对象一个新的列表。它起作用了。第一个问题很明显。您的诊断单元设置器是否发生了性能变化?谢谢GazedDestroyer。是的,我是<代码>公共int DiagnosticsUnit{get{return}deviceDiagnostics.DiagnosticsUnit;}set{if(value==\u deviceDiagnostics.DiagnosticsUnit)返回;\u deviceDiagnostics.DiagnosticsUnit=value;SetStatus();OnPropertyChanged(“DiagnosticsUnit”);}谢谢谢里丹。我不明白。。。如果是这样,为什么其他绑定(文本框)可以工作?这是完全相同的事情。DiagnosticsName和DiagnosticsCommand都是DeviceDiagnosticsViewModel中的属性,就像DiagnosticsUnit一样。唯一的区别是它们是字符串,而这一个是整数。你到底不明白什么?为什么其他绑定(文本框)对combobox有效而不是有效?从外观上看,它应该有效。。。也许你在什么地方有点拼写错误?否则,只需尝试使用
SelectedItem=“{Binding DiagnosticsUnit}”
。它总是比其他选择更有效。嗨。我试图将组合框从listview中取出,并使用属性将其绑定到dockpanel datacontext(DeviceDiagnosticsMainVeiwModel)并初始化,它成功了,因此我认为我没有任何语法问题。只有当它在listview中时才有问题,而我无法解决它。。。
public void GetItems()
{
_devicesDiagnostics = new ObservableCollection<DeviceDiagnosticsVeiwModel>(
(from dd in _dal.GetItems()
orderby dd.DeviceID
select new DeviceDiagnosticsVeiwModel(dd)
{
ObjectStatus = Status.NoChange,
Units = new ObservableCollection<UnitViewModel>(this.Units),
}
)
.ToObservableCollection<DeviceDiagnosticsVeiwModel>()
);
}