2010-08-04 104 views
0

我在WPF結合地獄:)我有一個公共類(Treatment)再次是如下:WPF - 如何綁定到一個依賴屬性自定義類的

public class Treatment() 
{ 
...  
    public Ticker SoakTimeActual; 
... 
} 

Ticker是一個依賴屬性:

public class Ticker : FrameworkElement 
{  
    // Value as string 
    public static readonly DependencyProperty DisplayIntervalProperty = DependencyProperty.Register("DisplayInterval", typeof(string), typeof(Ticker), null); 
    public string DisplayInterval 
    { 
     get { return (string)GetValue(DisplayIntervalProperty); } 
     set { SetValue(DisplayIntervalProperty, value); } 
    } 
    ... 
} 

在我的應用程序中,單個Treatment對象被創建並意味着是在XAML容易接近(在app.xaml):

<Application.Resources> 
    <ResourceDictionary> 
     <u:Treatment 
     x:Key="currentTreatment" /> 
    </ResourceDictionary> 
</Application.Resources> 

現在,我需要綁定到DisplayInterval依賴項屬性SoakTimeActual才能以當前狀態顯示此文本。這是我的嘗試,這是行不通的:

<TextBlock    
    Text="{Binding Source={StaticResource currentTreatment}, Path=SoakTimeActual.DisplayInterval}"/> 

這當然,編譯好了,但不會顯示任何東西。我假設我在更改通知或DataContext或兩者都出錯。

任何見識都被讚賞!

回答

3

WPF綁定僅對屬性操作,而不對字段操作。

因此,您需要SoakTimeActual字段更改爲一個屬性,像這樣:

public class Treatment 
{ 
...  
    public Ticker SoakTimeActual { get; set; } 
... 
} 
+0

作品!正確的。 – BabaBooey 2010-08-04 12:52:17

相關問題