如何在 c# wpf 中获取元素的标签? [英] How to get tag of an element in c# wpf?

查看:95
本文介绍了如何在 c# wpf 中获取元素的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WPF 构建一个 GUI,我可以在其中绘制一些基本形状并将它们存储到一个 xml 文件中.形状是在 xaml 文件中设计的,我为每个形状添加了标签.现在我想在我的代码中获取它们标签的值,以作为属性存储在输出 xml 文件中.

I am trying to build a GUI using WPF, in which I can draw some basic shapes and store them into a xml file. Shapes are designed in a xaml file, and I added tags for each of them. Now I want to get the value of their tags in my code to store as attributes in the output xml file.

例如,我在我的 xaml 文件中创建了一个带有名为RectangleTag"的标签的矩形形状,如下所示:

For instance, I created a rectangle shape with a tag named "RectangleTag" in my xaml file like this:

<Style x:Key="stack" TargetType="Rectangle" BasedOn="{StaticResource FlowChartRectangleStyle}"/>
    <Style x:Key="stack_DragThumb" TargetType="Rectangle" BasedOn="{StaticResource stack}">
    <Setter Property="IsHitTestVisible" Value="true"/>
    <Setter Property="Tag" Value="RectangleTag"/
</Style>

<Rectangle Style="{StaticResource stack}" ToolTip="stack" StrokeThickness="2">
        <s:DesignerItem.DragThumbTemplate>
        <ControlTemplate>
            <Rectangle Style="{StaticResource stack_DragThumb}" x:Name="StackShape" Tag="RectangleTag" />
        </ControlTemplate>
    </s:DesignerItem.DragThumbTemplate>
</Rectangle>

然后在我的代码中:

XElement myItem = new XElement("Items", 
        from item in designerItems
        let contentXaml = XamlWriter.Save(((DesignerItem)item).Conent)
        select new XElement("Item",
        new XAttribute( "Tag", item.Tag.ToString())
        );

然后我的 GUI 停止响应这一行.我相信一定有某种方法可以在这里获取标签,但显然不是以这种方式.我怎样才能做到这一点?它不一定是标签,还有 x:Namex:Key,它们足以让我区分给定的形状.

Then my GUI stops responding for this line. I believe there must be some way to get the tag here but not in this manner obviously. How can I do that? It won't necessarily be the tag, but also the x:Name or x:Key, that are enough to let me differentiate the given shapes.

我也试过这条线:

new XAttribute("Tag", item.Name)

但这给出了一个空字符串,而不是在 xaml 文件中分配的名称.有人可以帮忙吗?谢谢.

But this gives out an empty string, not the name that is assigned in the xaml file. Could someone help? Thanks.

推荐答案

正如 Sheridan 所说,您从错误的方向解决这个问题.

As Sheridan stated, you are attacking this problem from the wrong direction.

首先 - 如果您还没有阅读,请阅读:模型-视图-视图模型解释

First of all - required reading if you haven't yet: Model-View-ViewModel Explained

您应该创建一组用于定义形状的 Model 对象、一组将它们暴露给 View 并定义其行为的 ViewModel 对象,以及一个绑定到 ViewModel 的 View.

You should create a set of Model objects that define your shapes, a set of ViewModel objects that expose them to the View and define their behavior, and a View which binds to the ViewModel.

这样做的一个关键区别在于,现在您持久化到 XML 的逻辑根本不依赖于 UI,因此您不必尝试使用诸如 Tag 之类的东西来传递魔法值".

A key difference in doing it this way is that now your logic for persisting to XML is not dependent on the UI at all, so you won't have to try to use something like Tag to pass around 'magic values'.

顺便说一句,我发现绝大多数我都依赖于使用 Tag 来做任何事情,这表明我做错了.:)

And, as an aside, I have found that the vast majority I've relied on using Tag for anything, that has been an indicator that I'm Doing It Wrong. :)

这篇关于如何在 c# wpf 中获取元素的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆