WPF - 如何绑定到自定义类的依赖项属性

时间:2010-08-04 05:22:43

标签: data-binding dependency-properties

我再一次在WPF绑定地狱:)我有一个公共类(Treatment)如下:

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的{​​{1}}依赖项属性,以便以当前状态显示此文本。这是我的尝试,但不起作用:

SoakTimeActual

当然,这可以编译好,但不会显示任何内容。我假设我在更改通知或<TextBlock Text="{Binding Source={StaticResource currentTreatment}, Path=SoakTimeActual.DisplayInterval}"/> 或两者都犯了错误。

非常感谢任何见解!

1 个答案:

答案 0 :(得分:3)

WPF绑定仅对属性进行操作,而非字段。

因此,您需要将SoakTimeActual字段更改为属性,如下所示:

public class Treatment
{  
...    
    public Ticker SoakTimeActual { get; set; }
...  
}
相关问题