简体   繁体   中英

How do I make a C# object into a reusable component in WPF

I am trying to turn this C# class into some form of reusable component with WPF. The goal is to make some form of component I can pass a instance of AttributeBlock and that component will handle displaying and editing an instance of the class.

        public class AttributeBlock
        {
            public int Arcana;
            public int Foresight;
            public int Might;
            public int Resolve;
            public int Speed;
        }

What I would like to do looks something like this

<AttributeBlock Instance="some instance">

To my knowledge, there are two ways to do this with WPF, DataTemplate and UserControl . With a data template, I define how to display the object, but I cant edit the object (right?). Because DataTemplate is more about displaying a complex class that does not seem like it is the right fit for me. With a UserControl I have to re-define all of the fields of AttributeBlock as a dependency property and basically mirror the structure of AttributeBlock in the UserComponent. This is both repeating myself a lot, and is super verbose. What am I missing? Neither of these seem like a good tool to turn a class into some user interface, which seems like it should be a very common action in C# programming.

In your latter paragraph you answer your own question. Yes, you would build a UserControl and use dependency properties for this. You could also do it without dependency properties, but instead use the INotifyPropertyChanged interface, but I would rather recommend that for view models.

Simply put, WPF is sometimes cumbersome and requires a lot of code for simple stuff. I installed Dr. WPF's code snippets for faster creation of dependency properties, so tasks like yours are always quickly done.

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