[英]C# WPF Select UserControl on Canvas
I have a Canvas with custom UserControls in it. 我有一个带有自定义UserControls的画布。 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.
当我单击“画布”中的UserControl时,可能会有一些好处,可以将SelectedItem属性设置为该UserControl的viewmodel或更好的东西。 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. 当前,我有一个DocumentViewModel,它保存有关打开的文档/项目的信息。 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. 然后,我有一个DataTemplate,用于查看DocumentViewModel在视图中的外观。 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. ComponentViewModel是组件ViewModel的基类,这些ViewModel是围绕Model对象的简单包装。 The I use DataTemplates to bind them to a View so nothing special there.
我使用DataTemplates将它们绑定到View,所以没有什么特别的地方。
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? 那么,有没有人对如何使这些控件具有可单击性提出任何好的建议,以便我可以检测到选择了哪个控件,从而可以将其绑定到属性框?
而不是使用ItemsControl
而是使用具有选择的ListBox
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.