2010-09-06 46 views
0

我已经创建了自定义窗口控件(从Window继承),除了状态栏的文本外,一切都很好。我在控件中添加了一个名为“StatusText”的新属性,该文本以我的控件风格显示在TextBlock内。如何更新WPF自定义样式的一部分

但是,当我更改我的窗口的StatusText属性文本不会更改,它不会更新。另一方面,如果我更改窗口的标题属性(这是一个继承属性),标题会正确更改。

所以,也许我没有正确地宣布我的StatusText属性?或者我需要明确要求我的样式中的TextBlock更新?

感谢您的帮助。

状态文本财产申报:

private string m_StatusText; 

    public string StatusText 
    { 
     get { return m_StatusText; } 
     set { m_StatusText = value; } 
    } 

XAML样式状态栏:

<!-- Status area --> 
<Border Grid.Row="2" Style="{DynamicResource SFM_StatusAreaStyle}" CornerRadius="0, 0, 7, 7" BorderThickness="1, 1, 1, 0"> 
    <Grid Style="{DynamicResource SFM_TitleBarStyleReflect}"> 
      <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="6, 0, 0, 2" Foreground="{DynamicResource B_TextColor}" 
           Text="{Binding Path=StatusText, RelativeSource={RelativeSource AncestorType={x:Type local:SiluForm}, Mode=FindAncestor}}" /> 
    </Grid> 
</Border> 
+0

请问您可以发布您的代码吗?我假设你想使用数据绑定来完成文本更改,但没有人确切知道你在做什么,除非你发布代码/ XAML。 – Dave 2010-09-06 14:30:35

回答

1

StatusText类实现INotifyPropertyChanged然后插入rasing在StatusText二传手PropertyChanged事件的代码:

public class MyClass : INotifyPropertyChanged 
{ 
    private string m_StatusText; 

    public string StatusText 
    { 
     get { return m_StatusText; } 
     set 
     { 
      m_StatusText = value; 
      raiseOnPropertyChanged("StatusText"); 
     } 
    } 

    #region Implementation of INotifyPropertyChanged 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void raiseOnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     #endregion 
} 
+0

嗨,谢谢你的回答,但我无法继承INotifyPropertyChanged,因为我的控件已经继承了Window类。 C#不支持多继承权吗? – Karnalta 2010-09-06 19:54:32

+0

INotifyPropertyChanged是一个接口,C#类可以继承多个接口,因为它们指定了一个类必须包含的内容,而没有指定确切的功能。 – Val 2010-09-07 06:13:03

+0

谢谢像魅力一样工作。 – Karnalta 2010-09-07 06:40:04

0

除了像Eugene上面回答的那样实现INotifyPropertyChanged接口外,还可能需要在自定义窗口类构造函数中设置DataContext = this。那么你不应该需要RelativeSource绑定。

除非您将自定义窗口的DataContext用于其他目的。