简体   繁体   English

取消按钮写入文本框

[英]Cancel button writes to text box

I have a wpf application that prompts a user to enter some a numeric input for how many seconds to set stale time from a main window. 我有一个wpf应用程序,提示用户从主窗口输入一些数字输入,以设置秒数来设置失效时间。 The value is defaulted at 120, if the user wishes to change it he would click on the button and the a new instance of the window would come up and prompt the user to change it, I have the number validation part working, however if the user clicks the cancel button the current value is replaced by a blank(""), empty space, because that is technically the value in enter time window text box. 该值的默认值为120,如果用户希望对其进行更改,则可以单击该按钮,然后将出现一个新的窗口实例,并提示用户对其进行更改,我可以使用数字验证部分,但是如果用户单击“取消”按钮,当前值将被空白(“”)空格替代,因为从技术上讲,这是输入时间窗口文本框中的值。 How would I stop this from happening? 我将如何阻止这种情况的发生? I just want the window to close the current value to stay what it is. 我只希望窗口关闭当前值以保持原样。 NOTE this also happens if the user enters, say 3, into the box, the default value is replaced with 3 even though the user hit cancel. 注意如果用户在框中输入“ 3”,即使用户单击“取消”,默认值也将替换为3,也会发生这种情况。

Here is the WPF: 这是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>

Here is the C#: 这是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();
    }

}

This is the instance where it is called in C#: 这是在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();

    }

You need to bind that TextBox Text property in TwoWay mode to a public property supporting change notification in the DataContext of that View (typically a ViewModel) and set the UpdateSourceTrigger to LostFocus or Explicit . 您需要将TwoWay模式下的TextBox Text属性绑定到支持该视图的DataContext的更改通知的公共属性(通常是ViewModel),并将UpdateSourceTrigger设置为LostFocusExplicit

Once you do that all your problems will disappear and as a bonus you'll get rid of all that ugly code-behind... that's the WPF way of doing things. 一旦这样做,您所有的问题都将消失,此外,您还将摆脱所有丑陋的代码……这就是WPF的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM