我有一个ItemControl,它绑定到一个可观察集合的过滤视图。我希望能够像使用ListBoxItem一样选择ItemControl的项目。这样我就可以将ItemControl与现有ListBox的选择器同步。这可能吗?
这里是ItemControl的代码:
<ItemsControl ItemsSource="{Binding JointsModelPartView }" Grid.Row="1" Grid.Column="1">
<ItemsControl.ItemContainerStyle>
<Style TargetType="FrameworkElement">
<Setter Property="Canvas.Left" Value="{Binding posx1, Mode=TwoWay}"/>
<Setter Property="Canvas.Top" Value="{Binding posy1, Mode=TwoWay}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="Point">
<Ellipse Fill="AntiqueWhite"
Stroke="Black"
Width="10"
Height="10"
Margin="-5,-5,5,5"
DataContext="{StaticResource vm}"
Focusable="True"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<ei:CallMethodAction MethodName="Ellipse_SelectElement" TargetObject="{Binding}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<ei:CallMethodAction MethodName="Ellipse_MouseMove" TargetObject="{Binding}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeftButtonUp">
<ei:CallMethodAction MethodName="Ellipse_UnselectElement" TargetObject="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Ellipse.ContextMenu>
<ContextMenu ItemsSource="{Binding ContextActionsView}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Command" Value="{Binding RCommand}"/>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Ellipse.ContextMenu>
</Ellipse>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
这里是现有ListBoxItem的代码:
<ListBox Name="Model" ItemsSource="{Binding ModelPartView, Mode=OneWay}" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Margin="1,0,0,0" Padding="5,0">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding MPtype}" Grid.Row="0" Grid.Column="0" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding ContextActionsView}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Command" Value="{Binding RCommand}"/>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
我会对任何提示感到高兴。