我有一个wpf应用程序,提示用户输入一些数字输入,用于设置从主窗口设置过时时间的秒数。该值默认为120,如果用户希望更改它,他会点击按钮,窗口的新实例会出现并提示用户更改它,我有数字验证部分工作,但是如果用户单击取消按钮时,当前值将替换为空白(""),空格,因为这在技术上是输入时间窗口文本框中的值。我怎么能阻止这种情况发生?我只是希望窗口关闭当前值以保持原样。注意如果用户在框中输入(例如3),也会发生这种情况,即使用户点击取消,默认值也会替换为3。
这是WPF:
<Window x:Class="WpfClient.StaleTimeDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Stale Time" FontFamily="Arial" Width="450" Height="300" Topmost="True" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label Margin="10" FontSize="16">Enter new Stale Time</Label>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0, 10, 10, 0">
<TextBox Name="StaleTime" FontSize="20" Width="150" HorizontalAlignment="Left" VerticalContentAlignment="Center" Text="" AcceptsReturn="False" DataContext="{Binding}" PreviewTextInput="StaleTime_PreviewTextInput"></TextBox>
<Label Width="Auto" FontSize="20" Height="Auto" VerticalContentAlignment="Center">Seconds</Label>
</StackPanel>
<StackPanel Grid.Row="3" HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal">
<Button Name="SaveButton" Margin="10,10,10,10" Style="{StaticResource controlButtonStyle}" IsDefault="True" Click="SaveButton_Click">Save</Button>
<Button Name="CancelButton" Margin="10,10,10,10" Style="{StaticResource controlButtonStyle}" IsCancel="True" Click="CancelButton_Click">Cancel</Button>
</StackPanel>
</Grid>
这是C#:
public partial class StaleTimeDialog : Window
{
private Client client = null;
private SystemStatus systemStatus = null;
private UserTypes userType = UserTypes.Unknown;
private bool isStandaloneGUI = false;
private static string CurrentValue = "";
public StaleTimeDialog()
{
InitializeComponent();
CurrentValue = StaleTime.Text;
}
private void Cancel_Click(object sender, EventArgs e)
{
StaleTime.Text = CurrentValue.ToString();
CancelButton.IsCancel = true;
this.Close();
}
private void StaleTime_KeyPress(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
e.Handled = true;
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
if (true)
{
this.Close();
}
else
{
ErrorDialog errorDialog = new ErrorDialog(DialogType.OKDialog, IconType.Warning, "Save failed");
errorDialog.ShowDialog();
}
}
private void StaleTime_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
CheckIsNumeric(e);
}
private void CheckIsNumeric(TextCompositionEventArgs e)
{
int result;
if (!(int.TryParse(e.Text, out result) || e.Text == "."))
{
e.Handled = true;
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
StaleTime.Undo();
}
}
这是在C#中调用它的实例:
private void EditParametersButton_Click(object sender, RoutedEventArgs e)
{
StaleTimeDialog staletimeDialog = new StaleTimeDialog();
bool? result = staletimeDialog.ShowDialog();
if (staletimeDialog.StaleTime.Text.Equals("") || staletimeDialog.StaleTime.Text.Equals(" "))
{
App.ChipStale = DefaultStaleTime;
}
else
{
App.ChipStale = Convert.ToInt32(staletimeDialog.StaleTime.Text);
}
FoodParametersLine4Run.Text = App.ChipStale.ToString();
}
答案 0 :(得分:2)
您需要将TextBox
模式下的Text
TwoWay
属性绑定到支持该视图DataContext
中的更改通知的公共属性(通常是ViewModel)并设置UpdateSourceTrigger
到LostFocus
或Explicit
。
一旦你这样做,你所有的问题都将消失,作为奖励,你将摆脱所有丑陋的代码隐藏......这就是WPF的做事方式。