I've created a warning window to verify delete actions by the user, using Window.ShowDialog and setting the DialogResult. Everything works fine, except that the warning text doesn't appear in the TextBlock
and I don't know why. Here's my Window
:
<Window x:Class="RoVCo.Windows.VerifyWindow"
....
WindowStyle="None" Padding="10" ResizeMode="NoResize">
<StackPanel>
<TextBlock Height="Auto" Text="{Binding TBText, Mode=OneWay}" Foreground="Yellow" TextWrapping="Wrap" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
<Button Content="Cancel" Margin="10,0" Width="50" Click="CancelVerify" />
<Button Content="OK" Width="50" Click="ConfirmVerify" />
</StackPanel>
</StackPanel>
</Window>
And the class:
public partial class VerifyWindow : Window
{
public VerifyWindow(string content)
{
InitializeComponent();
_text = content;
}
private string _text = "";
public string TBText { get { return _text; } }
private void CancelVerify(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
private void ConfirmVerify(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
}
And I call it like such:
var window = new RoVCo.Windows.VerifyWindow("Removing this skill will erase all DP spent on it from all levels. Continue?");
if (window.ShowDialog() == false) return;
为了使用绑定,您需要本地属性是公共的,并且是通知属性或依赖项属性。
You can try this solution.
Change your VerifyWindow constructor to:
public VerifyWindow() { InitializeComponent(); }
and remove the TBText and _text code.
Create a new class called VerifyViewModel
public class VerifyViewModel : INotifyPropertyChanged { public VerifyViewModel(string content) { this.TBText = content; }
public string TBText { get; private set; } #region INPC code - can create an abstract base view model class and put this there instead public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { var handler = this.PropertyChanged; if (handler != null) { handler(this, e); } } #endregion
}
Call this code as below
var viewmodel = new VerifyViewModel ("Removing this skill will erase all DP spent on it from all levels. Continue?");
var window = new VerifyWindow { DataContext = viewmodel }; if (window.ShowDialog() == false) return;
If its just a single property that you are using in the Dialog
I think using a DependancyProperty
will be the better option then adding all the INotifyPropertyChanged
logic
public partial class VerifyWindow : Window
{
public VerifyWindow(string content)
{
InitializeComponent();
TBText = content;
}
public static readonly DependencyProperty TBTextProperty =
DependencyProperty.Register("TBText", typeof(string), typeof(VerifyWindow), new UIPropertyMetadata(string.Empty));
public string TBText
{
get { return (string)GetValue(TBTextProperty); }
set { SetValue(TBTextProperty, value); }
}
private void CancelVerify(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
private void ConfirmVerify(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.