1
我得到了下面的代碼爲我的目標(短版):爲什麼我的System.Windows.Data.Binding失敗並帶有DependencyProperty?
public class PluginClass
{
public int MyInt
{
get;
set;
}
public PluginClass()
{
Random random = new Random();
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += (sender, e) =>
{
MyInt = random.Next(0, 100);
}
}
}
然後我創建另一個類的int作爲一個DependencyProperty。下面是代碼(簡化版本太多)
public class MyClass : FrameworkElement
{
public int Value
{
get
{
return GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(MyClass), new PropertyMetadata(0));
public MyClass(object source, string propertyName)
{
var b = new System.Windows.Data.Binding();
b.Source = source;
b.Path = new PropertyPath(propertyName);
b.Mode = System.Windows.Data.BindingMode.TwoWay;
SetBinding(ValueProperty, b);
}
}
最後我創建插件類的一個實例,我想我的「敏」值綁定到的MyClass INT。這是我得到的(簡化版本)
PluginClass pc = new PluginClass();
MyClass mc = new MyClass(pc, "MyInt");
沒有編譯問題,但綁定無效。 總之,我不知道如果我theorically必須的了:
binding.Source = PluginClass.MyInt;
binding.Path = new PropertyPath("???"); // don't know what to "ask"
或
binding.Source = PluginClass;
binding.Path = new PropertyPath("MyInt");
我認爲第二個方法是很好的一個,但我不知道爲什麼它不」將不起作用:( 任何幫助將非常感激!
我正在考慮這個問題,但我總結說這是別的!會給它一個嘗試:) –
你是對的 - - thx! –