我有一个视图,其中2个列表视图绑定到在我的视图模型上从ObservableCollections创建的CollectionViewSources。
每个listview项都模板化为一个文本块和一个按钮 - 使用unitsInMeasureListView.ItemTemplate按钮绑定到一个命令,从该绑定集合中删除该项,并将其添加到availableUnitsListView上的集合中。
对于availableUnitsListView.ItemTemplate按钮,情况正好相反 - 它将它从availableUnitsListView中删除并将其添加到unitsInMeasureListView。
一切正常,项目将根据需要从每个列表视图中删除并添加。
当unitsInMeasureListView仍有焦点时,有没有办法可以自动选择添加到availableUnitsListView的项?
XAML:
<CollectionViewSource x:Key="unitsInMeasureViewSource" Source="{Binding Path=MeasureUnits}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="UnitInMeasure.IsPredefined" Direction="Descending" />
<componentModel:SortDescription PropertyName="UnitInMeasure.Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<CollectionViewSource x:Key="availableUnitsViewSource" Source="{Binding Path=AvailableUnits}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="UnitInMeasure.IsPredefined" Direction="Descending" />
<componentModel:SortDescription PropertyName="UnitInMeasure.Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ListView Grid.Row="1" x:Name="unitsInMeasureListView" BorderThickness="1" BorderBrush="Gray" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={StaticResource unitsInMeasureViewSource}}">
<ListView.ItemTemplate>
<DataTemplate>
<Border Margin="0 3" >
<DockPanel>
<TextBlock x:Name="unitItem" DockPanel.Dock="Left" Height="20" VerticalAlignment="Center" >
<TextBlock.Text>
<MultiBinding StringFormat=" {0} ({1})" >
<Binding Path="UnitInMeasure.Name" />
<Binding Path="UnitInMeasure.Symbol" />
</MultiBinding>
</TextBlock.Text>
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=UnitInMeasure.IsPredefined}" Value="false">
<Setter Property="TextBlock.FontStyle" Value="Italic"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}, Path=DataContext.EditUnitCommand}" CommandParameter="{Binding Path=UnitInMeasure}" />
</TextBlock.InputBindings>
</TextBlock>
<Button Command="{Binding Path=RemoveUnitFromMeasureCommand}" DockPanel.Dock="Right" HorizontalAlignment="Right" Style="{StaticResource TransparentStyle}" >
<Button.Visibility>
<MultiBinding Converter="{StaticResource multiBindingToVisibilityConverter}">
<Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
<Binding Path="IsMeasureEditable"/>
<Binding Path="CanRemoveUnit" />
</MultiBinding>
</Button.Visibility>
<Image Source="../../Images/Small/navigate_minus.png" ToolTip="Remove unit from measure" >
<Image.Visibility>
<MultiBinding Converter="{StaticResource multiBindingToVisibilityConverter}">
<Binding Path="IsFocused" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
</MultiBinding>
</Image.Visibility>
</Image>
</Button>
</DockPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView Grid.Row="3" x:Name="availableUnitsListView" BorderThickness="1" BorderBrush="Gray" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={StaticResource availableUnitsViewSource}}">
<ListView.ItemTemplate>
<DataTemplate>
<Border Margin="3" >
<DockPanel>
<TextBlock x:Name="measure" Height="20" VerticalAlignment="Center">
<TextBlock.Text>
<MultiBinding StringFormat=" {0} ({1})" >
<Binding Path="UnitInMeasure.Name" />
<Binding Path="UnitInMeasure.Symbol" />
</MultiBinding>
</TextBlock.Text>
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=UnitInMeasure.IsPredefined}" Value="false">
<Setter Property="TextBlock.FontStyle" Value="Italic"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<Button Command="{Binding Path=AddUnitToMeasureCommand}" DockPanel.Dock="Right" HorizontalAlignment="Right" Style="{StaticResource TransparentStyle}" >
<Button.Visibility>
<MultiBinding Converter="{StaticResource multiBindingToVisibilityConverter}">
<Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
<Binding Path="IsMeasureEditable"/>
</MultiBinding>
</Button.Visibility>
<Image Source="../../Images/Small/navigate_plus.png" ToolTip="Add unit to measure" >
<Image.Visibility>
<MultiBinding Converter="{StaticResource multiBindingToVisibilityConverter}">
<Binding Path="IsFocused" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
</MultiBinding>
</Image.Visibility>
</Image>
</Button>
</DockPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
视图模型上的绑定集合是:
/// <summary>
/// Gets a list of all unit view models currently within this measure.
/// </summary>
public ReadOnlyObservableCollection<UnitInMeasureViewModel> MeasureUnits
{
get
{
return this._measureUnits;
}
}
private readonly ReadOnlyObservableCollection<UnitInMeasureViewModel> _measureUnits;
private readonly ObservableCollection<UnitInMeasureViewModel> measureUnits;
/// <summary>
/// Gets a list of all unit view models currently available to this measure.
/// </summary>
public ReadOnlyObservableCollection<UnitInMeasureViewModel> AvailableUnits
{
get
{
return this._availableUnits;
}
}
private readonly ReadOnlyObservableCollection<UnitInMeasureViewModel> _availableUnits;
private readonly ObservableCollection<UnitInMeasureViewModel> availableUnits;
按钮命令绑定执行的视图模型代码:
/// <summary>
/// Adds a unit to this measure.
/// </summary>
/// <param name="vm">The view model for the unit to be added.</param>
internal void AddUnitToMeasure(UnitInMeasureViewModel vm)
{
this.availableUnits.Remove(vm);
this.measureUnits.Add(vm);
}
/// <summary>
/// Removes a unit from this measure.
/// </summary>
/// <param name="vm">The view model for the unit to be removed.</param>
internal void RemoveUnitFromMeasure(UnitInMeasureViewModel vm)
{
this.measureUnits.Remove(vm);
this.availableUnits.Add(vm);
}
答案 0 :(得分:0)
将ListBox中的SelectedItem绑定到View Model中的某个属性(比如X)。 每当您从ListBox中添加或删除项目(AddUnitToMeasure方法和RemoveUnitFromMeasure方法)时,您还需要更新此X属性以更新ListBox中的选定项目。