I open the window through the stream as follows.
Thread thread = new Thread(() => { AskReplace ask = new AskReplace(to); ask.Show(); Dispatcher.Run(); }); thread.SetApartmentState(ApartmentState.STA); thread.IsBackground = true; thread.Start(); Where AskReplace is the name of the window.
public AskReplace(string userFileToReplace) { InitializeComponent(); FileToReplace = userFileToReplace; ViewNameOfFile ="File "+userFileToReplace+"is exist do you want to replace it"; PropertyChanged +=OnPropertyChanged; } I sign the OnPropertyChanged method on the PropertyChanged event. The method itself looks like this.
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { FileText.Text = ViewNameOfFile; } Where FileText TextBlock looks like this.
<TextBlock Foreground="White" Margin="0,144,0,63" HorizontalAlignment="Center" Text="{Binding viewNameOfFile}" VerticalAlignment="Center" x:Name="FileText"/> and the ViewNameOfFile property of type string, which looks like this.
private string viewNameofFile; public string ViewNameOfFile { get { return viewNameofFile; } set { viewNameofFile = value; RunProperty("ViewNameOfFile"); } } PropertyChanged property I run through the RunProperty method, the method looks like this.
private void RunProperty(string propertyName) { //PropertyChanged +=OnPropertyChanged; если я подпишу метод OnPropertyChanged здесь то всё нормально.Я конечно могу так сделать,но мне бы хотелось бы знать с чем это может быть связано. var handler = PropertyChanged;//в этом месте почему-то PropertyChanged равен нулю хотя я его проинициализировал в конструкторе.Подписав на него метод OnPropertyChanged. if (handler!=null) handler.Invoke(this, new PropertyChangedEventArgs(propertyName)); } I suspect that this is somehow related to Dispatcher.Run () but not sure about that.
PropertyChangeddeclared and how exactly do you initialize it in the constructor? - PashaPash ♦Binding viewNameOfFile, by the way, is wrong, it is necessary to be attached to the property, and not to the field. - VladD