简体   繁体   中英

How to get tag of an element in c# wpf?

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.

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>

and

<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>

Then in my code I did:

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())
        );

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.

I also tried this line:

new XAttribute("Tag", item.Name)

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

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

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.

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'.

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. :)

Here is a example: assuming you have UI element is XAML ( Button named _ btn ) with Tag property set to some value, then in any event handle (eg Click) associated with that element in code behind you can get the Tag value as follows:

_btn.Click+=(s,e,)=>{ string _tag = (s as Button).Tag.ToString(); };

You can apply the same logic to you case. Rgds,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM