我有一個愚蠢的問題,我敢肯定很多人遇到。不過,我無法找到滿意的解決方案。wpf綁定事件後關閉窗口
考慮簡單的項目(請參閱下面的代碼)。當我在字段中鍵入文本並且單擊[x]
關閉窗口時,窗口的Closing事件之後出現VeryImportantProperty
的更改。結果窗口關閉而沒有要求保存更改。
是否有已知的解決方法或更好的編程技術?我發現的唯一建議是通過啓動一個具有較低優先級的子同步線程(無所事事)來延遲關閉處理程序。然而這並沒有太大的作用,因爲綁定和關閉事件處理程序在同一個線程中運行。
C#:
namespace CloseRequestTestProject {
public class MyViewModel : INotifyPropertyChanged {
public MyViewModel() { _isDirty = false; _veryImportantProperty = "Change me!"; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null) {
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
private bool _isDirty;
private string _veryImportantProperty;
public string VeryImportantProperty {
get { return _veryImportantProperty; }
set {
if (value != _veryImportantProperty) {
Trace.TraceWarning("Binding event!");
Trace.TraceWarning("ThreadId is " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
_isDirty = true;
_veryImportantProperty = value;
OnPropertyChanged("VeryImportantProperty");
}
}
}
public void viewIsClosing(object sender, CancelEventArgs e) {
Trace.TraceWarning("View is closing");
Trace.TraceWarning("ThreadId is " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
if (_isDirty) {
switch (MessageBox.Show("VeryImportantProperty has changed. Save changes?", "Question", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning)) {
case MessageBoxResult.Yes: ; break;
case MessageBoxResult.No: ; break;
default: e.Cancel = true; break;
}
}
}
}
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
MyViewModel vm = new MyViewModel();
DataContext = vm;
Closing += vm.viewIsClosing;
}
}
}
XAML:
<Window x:Class="CloseRequestTestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WrapPanel>
<TextBlock Text="Very Important Property" VerticalAlignment="Top" Margin="10"/>
<TextBox Text="{Binding VeryImportantProperty}" VerticalAlignment="Top" Margin="10" MinWidth="200"/>
</WrapPanel>
</Grid>
</Window>
是的,這確實有效。我想知道是否有其他解決方案。現實生活中的形式往往更爲複雜。 – user3088037
它的工作原理和有投票權? +1如果你有一個更復雜的現實生活形式,隨時發佈。這是對所述問題的正確答案。 – Paparazzi