C# WPF 在画布上选择 UserControl
我有一个带有自定义用户控件的画布。现在我希望能够选择它们,因为我想要一个属性框来显示有关该特定项目的信息。最好的是,当我单击画布中的 UserControl 时,可以将 SelectedItem 属性设置为该用户控件的视图模型或更好的东西。我只是不知道如何做好它,也没有成功地以任何方式让它发挥作用,这就是我在这里问的原因。
目前我有一个 DocumentViewModel,它保存有关打开的文档/项目的信息。在这个视图模型中,我有一个组件列表,这些组件是在画布上表示的。看起来像这样:
public class DocumentViewModel : BaseViewModel
{
private ObservableCollection<ComponentViewModel> components;
public ObservableCollection<ComponentViewModel> Components
{
get { return components; }
}
private string filePath;
public string FilePath
{
get { return filePath; }
set { filePath = value; }
}
...
}
然后我有一个 DataTemplate,用于说明 DocumentViewModel 在视图中的外观。看起来像这样:
<DataTemplate DataType="{x:Type ViewModels:DocumentViewModel}">
<DataTemplate.Resources>
<Converters:GuiSizeConverter x:Key="SizeConverter"/>
</DataTemplate.Resources>
<ItemsControl ItemsSource="{Binding Components}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas ClipToBounds="True" Height="{Binding CurrentProject.Height, Converter={StaticResource SizeConverter}}"
Width="{Binding CurrentProject.Width, Converter={StaticResource SizeConverter}}"
HorizontalAlignment="Left" VerticalAlignment="Top">
<Canvas.Background>
<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.WindowFrameColorKey}}"/>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Utils:DraggableExtender.CanDrag" Value="True" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y, Converter={StaticResource SizeConverter},Mode=TwoWay}" />
<Setter Property="Canvas.Left" Value="{Binding Path=X, Converter={StaticResource SizeConverter},Mode=TwoWay}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</DataTemplate>
ComponentViewModel 是我的组件 ViewModel 的基类,它是我的 Model 对象的简单包装器。我使用 DataTemplates 将它们绑定到视图,所以没有什么特别的。
那么,对于如何使这些控件可点击,以便我可以检测到选择了哪个控件,以便我可以将其绑定到属性框,有没有人有任何好的建议?
I have a Canvas with custom UserControls in it. Now I want to be able to have them selectable since I want to have a properties box which shows information about that specific item. What would be nice would be to have something along the way of when I click on a UserControl in the Canvas a SelectedItem property could be set to the viewmodel of that usercontrol or something better. I just have no clue how to do it good nor have I been successful in making it work in any way that why i'm asking here.
Currently I have a DocumentViewModel which holds information about the open document/project. In this viewmodel I have a list of components, which are the ones being represented on the canvas. This looks something like this:
public class DocumentViewModel : BaseViewModel
{
private ObservableCollection<ComponentViewModel> components;
public ObservableCollection<ComponentViewModel> Components
{
get { return components; }
}
private string filePath;
public string FilePath
{
get { return filePath; }
set { filePath = value; }
}
...
}
Then I have a DataTemplate for how the DocumentViewModel should look in the View. This looks like this:
<DataTemplate DataType="{x:Type ViewModels:DocumentViewModel}">
<DataTemplate.Resources>
<Converters:GuiSizeConverter x:Key="SizeConverter"/>
</DataTemplate.Resources>
<ItemsControl ItemsSource="{Binding Components}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas ClipToBounds="True" Height="{Binding CurrentProject.Height, Converter={StaticResource SizeConverter}}"
Width="{Binding CurrentProject.Width, Converter={StaticResource SizeConverter}}"
HorizontalAlignment="Left" VerticalAlignment="Top">
<Canvas.Background>
<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.WindowFrameColorKey}}"/>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Utils:DraggableExtender.CanDrag" Value="True" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y, Converter={StaticResource SizeConverter},Mode=TwoWay}" />
<Setter Property="Canvas.Left" Value="{Binding Path=X, Converter={StaticResource SizeConverter},Mode=TwoWay}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</DataTemplate>
The ComponentViewModel is a base class for my component ViewModels which are simple wrappers around my Model objects. The I use DataTemplates to bind them to a View so nothing special there.
So does anyone have any good suggestions for how to make these controls clickable so i can detect which one is selected so i can bind that to a properties box?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不使用
ItemsControl
,只需使用ListBox
,具有选择功能。Instead of using an
ItemsControl
just use aListBox
, which has selection.