如何创建此共享类成员或属性

时间:2014-05-27 07:19:42

标签: c# wpf properties dependency-properties

我有以下由我的XAML引用的类。这个类包含许多属于按钮的附加属性和行为,因此它位于UI的一侧。

其中一些行为设置了current_cell_match,并且每个行为都有自己的类,这就是我将它放入静态类以便可以共享的原因。

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

我不确定current_cell_match是否应该是会员,财产或附属财产。我并不需要将它作为任何UI控件的属性使用,因此我想到了前两个中的一个。

我将使用MultiBinding转换器,所以我需要知道它的值何时发生变化。所以我倾向于使用PropertyChangedEventHandler的属性。然而,静态类没有实例,所以这不会起作用,没有'这个'。

public event PropertyChangedEventHandler PropertyChanged;

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

这是我将在最后使用的多重绑定:

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

有人可以帮我构建current_cell_match吗?

2 个答案:

答案 0 :(得分:1)

关于“但是静态类没有实例,所以这不起作用,没有'这个'。”你可以做到以下几点:

public event PropertyChangedEventHandler PropertyChanged;

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

这将向PropertyChanged-listeners指示发件人不是实例,并且侦听器仍然能够看到发件人类型。

要更新视图,您需要实现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
}

快乐编码: - )

答案 1 :(得分:1)

您说我将使用MultiBinding转换器,因此基本上限制了您使用DependencyProperty或附加属性的选项。来自MSDN上的Custom Dependency Properties页:

  

何时应实施依赖属性?
  ...
  •您希望您的财产支持数据绑定   ......

但是,由于附加属性实际上用于扩展预先存在的UI控件的功能,所以您的选择现在似乎很简单......您剩下的唯一选择是使用DependencyProperty。从MSDN上的自定义依赖项属性页面:

  

何时创建附加资产?
  如果有理由为定义类以外的类提供属性设置机制,则可以创建附加属性。最常见的情况是布局。现有布局属性的示例是DockPanel.Dock,Panel.ZIndex和Canvas.Top。此处启用的方案是作为布局控制元素的子元素存在的元素能够单独向布局父元素表达布局要求,每个元素设置父级定义为附加属性的属性值。

     

使用附加属性的另一种情况是,当您的类表示服务时,您希望类能够更透明地集成服务。
  ......

从技术上讲,您可以使用附加属性,但如果没有使用UI,它将不会为您提供任何额外的功能,同时使用起来比简单的DependencyProperty更难。另外,为了您的信息,您应该阅读程序员论坛上Is it bad practice to use public fields?问题的接受答案。

相关问题