簡體   English   中英

取消按鈕寫入文本框

[英]Cancel button writes to text box

我有一個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();

    }

您需要將TwoWay模式下的TextBox Text屬性綁定到支持該視圖的DataContext的更改通知的公共屬性(通常是ViewModel),並將UpdateSourceTrigger設置為LostFocusExplicit

一旦這樣做,您所有的問題都將消失,此外,您還將擺脫所有丑陋的代碼……這就是WPF的工作方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM