如何创建此共享的类成员或属性 [英] How to create this shared class member or property
问题描述
我有XAML引用的以下类。该类带有大量属于按钮的附加属性和行为,因此它位于UI方面。
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.
其中一些行为设置了current_cell_match,并且每个行为都有其自己的类,这就是为什么我将其置于静态类中以便可以共享。 / p>
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);
}
}
我不确定current_cell_match是否应为成员,属性或附加财产。我真的不需要将其用作任何UI控件上的属性,因此我在考虑前两个。
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.
我将它与MultiBinding转换器一起使用,因此我需要知道它的值何时更改。因此,我倾向于使用具有PropertyChangedEventHandler的属性。但是静态类没有实例,因此这将不起作用,没有 this。
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>
有人可以帮我构建current_cell_match吗?
Could someone help me construct current_cell_match please?
推荐答案
关于但是,静态类没有实例,所以它不会起作用,没有'this'。您可以执行以下操作:
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));
}
这将向PropertyChanged侦听器指示发件人不是实例,
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.
要更新视图,您需要实现INotifyPropertyChanged接口。但这在使用静态属性时可能会非常棘手。相反,我建议实现单例模式,并使您的静态属性成为正常属性。静态类和单例模式之间的差异不是很大。因此,这可能是您要走的路。
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.
这里是一个示例。 Xaml:
Here is an example. 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
}
快乐编码:-)
这篇关于如何创建此共享的类成员或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!