C# WPF 在画布上选择 UserControl

发布于 2024-10-29 04:45:16 字数 2633 浏览 3 评论 0原文

我有一个带有自定义用户控件的画布。现在我希望能够选择它们,因为我想要一个属性框来显示有关该特定项目的信息。最好的是,当我单击画布中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮凉 2024-11-05 04:45:17

不使用 ItemsControl,只需使用 ListBox,具有选择功能。

Instead of using an ItemsControl just use a ListBox, which has selection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文