How to create this shared class member or property

ε祈祈猫儿з 提交于 2019-12-13 05:46:38

问题


I have the following class that is referenced by my XAML. This class sits with a bunch of attached properties and behaviors that belong to buttons, so it is on the UI side of things.

A few of these behaviors set current_cell_match and each of these behaviors has it's own class, that is why I placed it into a static class so that it can be shared.

public static class SearchVariables
{
    public static DataGridCellInfo current_cell_match;

    public static void setCurrentCell(Object dgi, DataGridColumn dgc, string property_name)
    {
        current_cell_property = property_name;
        if (property_name == null)
            current_cell_match = new DataGridCellInfo();
        else
            current_cell_match = new DataGridCellInfo(dgi, dgc);
    }
}

I am unsure if current_cell_match should be a member, property or attached property. I don't really need to use it as a property on any of the UI controls so I'm thinking one of the first two.

I will be using it with a MultiBinding converter, so I need to know when it's value changes. So I'm leaning towards a property with a PropertyChangedEventHandler. However static classes don't have an instance, so this won't work, there is no 'this'.

public event PropertyChangedEventHandler PropertyChanged;

private void RaisePropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}

Here is the multibinding I will use in the end:

<Setter Property="helpers:SearchBehaviours.IsTextMatchFocused">
    <Setter.Value>
        <MultiBinding Converter="{StaticResource SelectedSearchValueConverter}" FallbackValue="False">
            <Binding Path="(helpers:SearchBehaviours.IsFindPopupOpen)" RelativeSource="{RelativeSource Self}"/>
            <Binding Source="{x:Static helpers:SearchVariables.current_cell_match}"/>
        </MultiBinding>
    </Setter.Value>
</Setter>

Could someone help me construct current_cell_match please?


回答1:


Regarding "However static classes don't have an instance, so this won't work, there is no 'this'." you can do the following:

public event PropertyChangedEventHandler PropertyChanged;

private void RaisePropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(typeof(this), new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}

This will indicate to the PropertyChanged-listeners that the sender is not an instance, and the listeners will still be able to see the sender type.

To update the view you need to implement the interface INotifyPropertyChanged. But this can be pretty tricky when using static properties. Instead I would suggest to implement the singleton pattern, and make your static properties "normal" properties. The differences between a static class and the singleton pattern are not that big. So this might be the way for you to go.

Here is an example. Xaml:

<Binding Source="{x:Static local:MyClass.Instance}" Path="MyInt" />

Code:

public class MyClass : INotifyPropertyChanged
{
    private Random random;

    private int m_MyInt;
    public int MyInt
    {
        get
        {
            return m_MyInt;
        }
        set
        {
            if ( m_MyInt == value )
            {
               return;
            }

            m_MyInt = value;
            NotifyPropertyChanged();
        }
    }

    private static MyClass m_Instance;
    public static MyClass Instance
    {
        get
        {
            if ( m_Instance == null )
            {
                m_Instance = new MyClass();
            }

            return m_Instance;
         }
    }

    private MyClass()
    {
        random = new Random();
        m_MyInt = random.Next( 0, 100 );

        Timer timer = new Timer();
        timer.Interval = 1000;
        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    private void timer_Elapsed( object sender, ElapsedEventArgs e )
    {
        MyInt = random.Next( 0, 100 );
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged( [CallerMemberName] String propertyName = "" )
    {
        if ( PropertyChanged != null )
        {
            PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
        }
    }

    #endregion
}

Happy coding :-)




回答2:


You said I will be using it with a MultiBinding converter, so that basically limits your options to using a DependencyProperty or an Attached Property. From the Custom Dependency Properties page on MSDN:

When Should You Implement a Dependency Property?
...
• You want your property to support data binding.
...

However, as Attached Properties are really used for extending the functionality of pre-existing UI controls, it seems that your choice is now simple... the only option that you have left is to use a DependencyProperty. From the Custom Dependency Properties page on MSDN:

When to Create an Attached Property?
You might create an attached property when there is a reason to have a property setting mechanism available for classes other than the defining class. The most common scenario for this is layout. Examples of existing layout properties are DockPanel.Dock, Panel.ZIndex, and Canvas.Top. The scenario enabled here is that elements that exist as child elements to layout-controlling elements are able to express layout requirements to their layout parent elements individually, each setting a property value that the parent defined as an attached property.

Another scenario for using an attached property is when your class represents a service, and you want classes to be able to integrate the service more transparently.
...

Technically speaking, it would be possible for you to use an Attached Property, but without UI use, it would not provide any extra functionality for you, while being more awkward to use than a simple DependencyProperty. Also, for your information, you should read the accepted answer to the Is it bad practice to use public fields? question on the Programmers forum.



来源:https://stackoverflow.com/questions/23883306/how-to-create-this-shared-class-member-or-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!