2010-09-06 51 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用於其他目的。