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

前端 未结 2 1055
一个人的身影
一个人的身影 2021-01-28 03:12

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

2条回答
  •  萌比男神i
    2021-01-28 03:46

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

提交回复
热议问题