Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference static class field from XAML

I have the following class that is referenced by my XAML:

public static class SearchVariables
{
    public static DataGridCellInfo current_cell_match;
    public static string current_cell_property;

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

What I would like to do is set up a MultiBinding Converter that uses current_cell_match when it changes. I have the following but it throws an error could use some help to resolve this.

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

[EDIT]

Should have mentioned that this class sits with a bunch of attached properties and behaviors, so it is on the UI side of things. One of these behaviors sets current_cell_match.

like image 322
Hank Avatar asked May 26 '14 07:05

Hank


2 Answers

To make a binding to a static property in a static class try the following:

<Binding Source="{x:Static helpers:SearchVariables.current_cell_match}" />

But this will not update in the view when the values are changing. 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 :-)

like image 154
Casper Taylor Korshøj Avatar answered Oct 16 '22 18:10

Casper Taylor Korshøj


Static members can be problematic in terms of PropertyChanged. Please take a look at the question I asked a short time ago:

x:Static value in Control does not update

Mabe x:Shared will help you.

like image 1
Gope Avatar answered Oct 16 '22 17:10

Gope