2014-07-04 8 views
6

मैं TextTextBlock की संपत्ति को अपनी संपत्ति में बांधने का प्रयास करता हूं लेकिन टेक्स्ट अपडेट नहीं होता है।डब्ल्यूपीएफ टेक्स्टब्लॉक बाइंडिंग

XAML

<Window x:Name="window" x:Class="Press.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
    Title="Press analyzer" Height="350" Width="525" ContentRendered="Window_ContentRendered" 
    d:DataContext="{d:DesignData MainWindow}"> 
... 
    <StatusBar Name="StatusBar" Grid.Row="2" > 
     <TextBlock Name="StatusBarLabel" Text="{Binding Message}"/> 
    </StatusBar> 
</Window> 

सी #

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    private string _message; 
    public string Message 
    { 
     private set 
     { 
      _message = value; 
      OnPropertyChanged("Message"); 
     } 
     get 
     { 
      return _message; 
     } 
    } 
public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+1

आप डेटाकॉन्टेक्स्ट कहां सेट कर रहे हैं? – Sajeetharan

+0

@Sajeetharan I thint htis 'd: DataContext =" {d: DesignData MainWindow} ' –

उत्तर

8

MainWindow के निर्माता में खुद को DataContext MainWindow की सेट बंधन को हल करने:

public MainWindow() 
{ 
    InitializeComponent(); 
    this.DataContext = this; 
} 
तुम हमेशा जाने के लिए और उत्पादन की जांच कर सकते -

<TextBlock Name="StatusBarLabel" 
      Text="{Binding Message, RelativeSource={RelativeSource 
            Mode=FindAncestor, AncestorType=Window}}"/> 

नोट: 10

या

आप DataContext निर्धारित नहीं करते हैं, तो आप RelativeSource का उपयोग कर XAML से स्पष्ट रूप से बाध्यकारी को हल करने के लिए है किसी भी बाध्यकारी त्रुटियों के लिए विजुअल स्टूडियो की खिड़की।

+1

धन्यवाद है, मैंने दूसरा तरीका उपयोग किया और यह काम करता है। –

संबंधित मुद्दे