views:

25

answers:

2

Hi all,

I have created a custom window control (inherited from Window), all is fine except the text of my status bar. I have added a new property to my control called "StatusText" and this text is shown inside a TextBlock in my control's style.

But when I change the StatusText property of my window the text doesn't change, it's not update. On another side, if I change the Title property of my window (which is an inherited property) the Title change correctly.

So maybe I have not correctly declared my StatusText property ? Or I need to explicitly ask for the TextBlock in my style to update ?

Thank for your help.

StatusText Property Declaration :

    private string m_StatusText;

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

XAML Style for the status bar :

<!-- 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>
+1  A: 

Implement INotifyPropertyChanged in your class containing StatusText and then insert code of rasing PropertyChanged event in setter of StatusText:

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
}
Eugene Cheverda
Hi and thank for your answer, but I can't inherit INotifyPropertyChanged as my control already inherit the Window class. C# doesn't support multiple inheritance right ?
Karnalta
INotifyPropertyChanged is an interface, C# classes can inherit from multiple interfaces, as they specify what a class must contain without specifying exact functionality.
Val
Thank worked like a charm.
Karnalta
A: 

In addition to implementing the INotifyPropertyChanged interface as Eugene answered above, you might also need to set DataContext = this in your custom window classes constructor. Then you shouldn't need the RelativeSource binding.

Unless you're using the DataContext of the custom window for other purposes.

Val