在两个类之间共享C#(WPF)中的依赖项属性 [英] Sharing dependency property in C# (WPF) between two classes
问题描述
我希望两个人在使用 AddOwner
的类之间共享一个 DepedencyProperty
(欢迎使用其他任何方法),例如
I want two share a DepedencyProperty
between to classes using AddOwner
(any other approach is welcome), e.g.
class ClassA : DependencyObject
{
public int Number
{
get { return (int)GetValue(NumberProperty); }
set { SetValue(NumberProperty, value); }
}
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register("Number", typeof(int), typeof(ClassA),
new FrameworkPropertyMetadata(0,
FrameworkPropertyMetadataOptions.Inherits));
}
和
class ClassB : DependencyObject
{
public int Number
{
get { return (int)GetValue(NumberProperty); }
set { SetValue(NumberProperty, value); }
}
public static readonly DependencyProperty NumberProperty =
ClassA.NumberProperty.AddOwner(typeof(ClassB),
new FrameworkPropertyMetadata(0,
FrameworkPropertyMetadataOptions.Inherits));
}
就像描述的此处。您可能会猜到:当然不行。这是完全合理的,因为将不可能创建同一个类的多个实例,这些实例都具有自己的依赖属性。
like described here. As you might guess: Of course it doesn't work. That makes perfect sense, because it would make it impossible to create multiple instances of the same class that all have their "own" dependency property.
如何确保 ClassA
, ClassB $ c $的所有类(尤其是所有实例) c>和其他任何引用该属性的类都在谈论完全相同的属性(因此是 value )? Singleton 是不可选项,因为
A类
是 MainWindow
,而 Class B
是 UserControl
(因此无法使用受保护的构造函数)。
How do I make sure that all classes (and especially all instances) of ClassA
, ClassB
and any other class which refers to the property are talking about the exact same property (and therefore value)? A Singleton is no option, since Class A
is a MainWindow
and Class B
is an UserControl
(protected constructors are therefore not possible).
致谢,
Ben
Regards, Ben
推荐答案
我认为您误解了DependencyProperties的目的。
I think you're misunderstanding the purpose of DependencyProperties.
它们基本上是属性定义,没有属性 Value 。
They are basically a Property Definition, without a property Value.
它们定义名称,类型,默认值,值的位置等内容,但是它们本身并不包含实际值。这样可以为该值提供指向任何其他位置的任何其他属性的绑定。
They define things like name, type, default value, location of the value, etc however they do not contain the actual value itself. This allows the value to be provided with a binding pointing to any other property in any other location.
您最好的选择是创建一个由a作为后盾的属性
Your best bet is to probably just create a property that is backed by a singleton property.
public int Number
{
get { return MySingleton.Number; }
set { MySingleton.Number = value; }
}
编辑
基于下面的注释,您要使对象的所有实例响应其他任何对象的更改通知,因此您想实现 INotifyPropertyChanged 并订阅它的 PropertyChange $ c每个使用该值的类中的$ c>事件。
Based on comments below where you say you want all instances of the object to respond to change notifications from any of the other objects, you'd want to implement INotifyPropertyChanged on your singleton object, and subscribe to it's PropertyChange
event in each class that uses that value.
例如,
public ClassA
{
public ClassA()
{
MySingleton.PropertyChanged += Singleton_PropertyChanged;
}
void Singleton_PropertyChanged(object sender, NotifyPropertyChangedEventArgs e)
{
// if singleton's Number property changed, raise change
// notification for this class's Number property too
if (e.PropertyName == "Number")
OnPropertyChanged("Number");
}
public int Number
{
get { return MySingleton.Number; }
set { MySingleton.Number = value; }
}
}
这篇关于在两个类之间共享C#(WPF)中的依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!