wpf 如何从 XAML 引用静态类字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23864495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference static class field from XAML
提问by Hank
I have the following class that is referenced by my 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);
}
}
}
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.
我想要做的是设置一个多绑定转换器,当它改变时使用 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>
[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.
应该提到这个类有一堆附加的属性和行为,所以它是在 UI 方面。这些行为之一设置 current_cell_match。
回答by Casper Taylor Korsh?j
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.
但是当值发生变化时,这不会在视图中更新。要更新视图,您需要实现接口 INotifyPropertyChanged。但这在使用静态属性时可能非常棘手。相反,我建议实现单例模式,并使您的静态属性成为“正常”属性。静态类和单例模式之间的差异并不大。所以这可能是你要走的路。
Here is an example. Xaml:
这是一个例子。Xml:
<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 :-)
快乐编码:-)
回答by Gope
Static members can be problematic in terms of PropertyChanged. Please take a look at the question I asked a short time ago:
静态成员在 PropertyChanged 方面可能有问题。请看一下我不久前问的问题:
x:Static value in Control does not update
Mabe x:Shared will help you.
Mabe x:Shared 会帮助你。

