简体   繁体   中英

Attaching anonymous properties in object?

I'm working in a WPF project, it has a numerous number of user control. for some forms , when the user close the container tab, a confirmation close is shown up, for other forms they are just closed. so I use the

FrmAccounts FrmAcc = new FrmAccounts {Tag = "showConfirmClose"};

to decide what tab I need to close.

and in the closing event I check if the tag is set to showConfirmClose to show the confirmation message. But I don't like using Tag it's not good in C#, also I thought, what if I want to send more data (the only solution will be to comma separating them in the Tag and Split them, but this is worse).

I can't find a good, performant way to accomplish such task: if this is possible:

FrmAccounts FrmAcc = new FrmAccounts {new{ShowConfirmClose= true }};

Attached property

You can use an AttachedProperty to store the additional information for your objects. However, these object have to be DependencyObject s, otherwise it won't work.

Here is an example.

Define your attached property in a separate class (it can be a simple static class or a DependencyObject ):

namespace YourNamespace
{
    public static class CloseConfirmation
    {
        public static readonly DependencyProperty IsActiveProperty = DependencyProperty.RegisterAttached(
           "IsActive",
           typeof(bool),
           typeof(CloseConfirmation));

        public static bool GetIsActive(DependencyObject obj)
        {
            return (bool)obj.GetValue(HiddenProperty);
        }

        public static void SetIsActive(DependencyObject obj, bool value)
        {
            obj.SetValue(HiddenProperty, value);
        }
    }
}

In XAML, set the attached property value for your objects (WPF Window s, Page s and other DependencyObjects ):

<FrmAccounts xmlns:yns="YourNamespace"
    yns:CloseConfirmation.IsActive="true">
    <!-- Content -->
</FrmAccounts>

You can use this attached property in your XAML definition, eg in triggers.

You can also access its value in code-behind:

FrmAccounts frm = new FrmAccounts();
CloseConfirmation.SetIsActive(frm, true);
var isActive = CloseConfirmation.GetIsActive(frm); // true

You can create an attached property not only for a simple type like bool or int , but also for any custom type. So use your own container class if you need to transfer a lot of data.

Tag object

Using Tag s is fully OK in .NET. Virtually any UI type has this property, so that you can attach objects of any type to your UI elements. However, these types cannot be anonymous types. You have to create your own container type:

class Container
{
    public bool IsConfirmationNeeded { get; }
    public IEnumerable<IItem> MyData { get; } // any data you need to pass
}

FrmAccounts frm = new FrmAccounts();
frm.Tag = new Container();

var container = (Container)frm.Tag;
var confirmation = container.IsConfirmationNeeded; // false by default

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