C# 要使用哪个WPF控件而不是ItemsControl来创建画布,其中包含绑定到ObservableCollection的可自由放置的项?
我正在开发一个GUI,用户可以根据自己的意愿在画布上放置项目,并处理这些项目。目前,我使用C# 要使用哪个WPF控件而不是ItemsControl来创建画布,其中包含绑定到ObservableCollection的可自由放置的项?,c#,wpf,C#,Wpf,我正在开发一个GUI,用户可以根据自己的意愿在画布上放置项目,并处理这些项目。目前,我使用ItemsControl解决了这个问题,如下所示: <ItemsControl ItemsSource="{Binding PlacementConfiguration.EquipmentPlacementList}" Background="AliceBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <
ItemsControl
解决了这个问题,如下所示:
<ItemsControl ItemsSource="{Binding PlacementConfiguration.EquipmentPlacementList}" Background="AliceBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Position.X}"/>
<Setter Property="Canvas.Top" Value="{Binding Position.Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Width="150" Height="100" BorderThickness="3">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0" >
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="Black" Offset="0.1"/>
<GradientStop Color="Transparent" Offset="0.1"/>
<GradientStop Color="Transparent" Offset="0.9"/>
<GradientStop Color="Black" Offset="0.9"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Label Content="TestLabel, Content will be presented here!"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
因此,这些项被绑定到viewmodel中的集合。每个项都由一个具有多个属性和选项的类实例表示。上述解决方案的问题是,ItemsControl
项不可选择,这是我需要的功能
我想到的解决方案要么是有一种替代ItemsControl
的方法,它给了我同样的自由,同时能够选择项目,要么使用不同的鼠标事件手动解决
问题,哪种解决方案更可行……或者,是否有完全其他(更好)的方式来解决这样的问题 ListBox是一个选择器ItemsControl@ASh测试出
ListBox
(我也测试了ListView
,但是使用上面的代码我得到了一个异常“针对“ContentPresenter”类型的样式无法应用于“ListBoxItem”类型。我必须研究导致此异常的原因将样式应用于ListBoxItem,这是ListBoxAhhh的ItemContainer,事实上非常明显,就是这样做的-感谢您的帮助!ListBoxItem是一个选择器ItemsControl@ASh测试出ListBox
(我也测试了ListView
,但是上面的代码有一个例外“针对“ContentPresenter”类型的样式无法应用于“ListBoxItem”类型。我必须研究导致此异常的原因将样式应用于ListBoxItem,这是ListBoxAhhh的ItemContainer,事实上非常明显,确实如此-感谢您的帮助!