繁体   English   中英

如何从XAML引用静态类字段

[英]How to reference static class field from XAML

我有我的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);
        }
    }
}

我想要做的是设置一个MultiBinding Converter,它在更改时使用current_cell_match。 我有以下但它抛出一个错误可以使用一些帮助来解决这个问题。

<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>

[编辑]

应该已经提到过这个类带有一堆附加的属性和行为,所以它在UI的一面。 其中一个行为设置current_cell_match。

要在静态类中绑定静态属性,请尝试以下操作:

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

但是当值发生变化时,这不会在视图中更新。 要更新视图,您需要实现INotifyPropertyChanged接口。 但是在使用静态属性时这可能非常棘手。 相反,我建议实现单例模式,并使您的静态属性“正常”属性。 静态类和单例模式之间的差异并不大。 所以这可能是你走的路。

这是一个例子。 XAML:

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

码:

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
}

快乐的编码:-)

静态成员在PropertyChanged方面可能存在问题。 请看一下我不久前问过的问题:

x:Control中的静态值不会更新

Mabe x:共享会帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM