c# - 创建我创建的窗口的新实例。多次调用做不同的事情

标签 c# wpf class xaml textbox

我创建了一个窗口,它是一个数字键盘:

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <TextBox Name="btnTotal" Width="280" Height="60" Grid.ColumnSpan="3" Grid.Row="0" Margin="10,10,10,10" Background="#302F37" Foreground="AntiqueWhite" FontSize="35"></TextBox>
    <Button x:Name="btnZzero" Content="0" Width="80" Height="60" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="#302F37" Foreground="White" Focusable="False"  Click="btnZzero_Click"></Button>
    <Button x:Name="btnOk" Content="OK" Width="80" Height="60" Grid.Column="1" Grid.Row="4" Margin="5,5,5,5" Click="btnOk_Click" Background="#FF8FC377" Focusable="False"></Button>
    <Button x:Name="btnCancel" Content="Cancel" Width="80" Height="60" Grid.Column="2" Grid.Row="4" Margin="5,5,5,5" Click="cancel_Click" BorderBrush="Black" Background="#FFD64D4D" Focusable="False"></Button>
    <Button x:Name="btnOne" Content="1" Width="80" Height="60" Grid.Column="0" Grid.Row="3" Margin="14,6,0,6" Focusable="False" Background="#302F37" Foreground="White" HorizontalAlignment="Left" Click="btnOne_Click"></Button>
    <Button x:Name="btnTwo" Content="2" Width="80" Height="60" Grid.Column="1" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnTwo_Click"/>
    <Button x:Name="btnThree" Content="3" Width="80" Height="60" Grid.Column="2" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"/>
    <Button x:Name="btnFour" Content="4" Width="80" Height="60" Grid.Column="0" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
    <Button x:Name="btnFive" Content="5" Width="80" Height="60" Grid.Column="1" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
    <Button x:Name="btnSix" Content="6" Width="80" Height="60" Grid.Column="2" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
    <Button x:Name="btnSeven" Content="7" Width="80" Height="60" Grid.Column="0" Grid.Row="1" Margin="12,5,9,6" Focusable="False" Background="#302F37" Foreground="White"></Button>
    <Button x:Name="btnEight" Content="8" Width="80" Height="60" Grid.Column="1" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>
    <Button x:Name="btnNine" Content="9" Width="80" Height="60" Grid.Column="2" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White"></Button>

现在,我想在我的应用程序中的不同时间调用这个键盘来做不同的事情。

IE 我想将它用于登录屏幕:

<Window x:Class="WpfApplication2.MainLogin2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="MainLogin2" WindowState="Maximized" Width="1024" Height="768" WindowStyle="None" WindowStartupLocation="CenterScreen">
<Grid Background="#FF260538">
    <Grid.RowDefinitions>
        <RowDefinition Height="768" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1024" />
    </Grid.ColumnDefinitions>
    <TextBox Name="userIDTextBox" HorizontalAlignment="Left" Height="47" Margin="402,249,0,0" TextWrapping="Wrap" Text="User ID" VerticalAlignment="Top" Width="213" FontSize="30" PreviewMouseDown="userIDTextBox_PreviewMouseDown"  />
    <TextBox Name="passcodeTextBox" Height="47" Margin="402,317,0,0" TextWrapping="Wrap" Text="Passcode" VerticalAlignment="Top" Width="213" FontSize="30" HorizontalAlignment="Left" PreviewMouseDown="passcodeTextBox_PreviewMouseDown" />

    <Button Content="OK" HorizontalAlignment="Left" Margin="402,386,0,0" VerticalAlignment="Top" Width="213" Height="59" FontSize="30" Click="okButton_Click"/>
    <Button Content="CANCEL" HorizontalAlignment="Left" Margin="402,450,0,0" VerticalAlignment="Top" Width="213" Height="59" FontSize="30" Click="cancelButton_Click_1"/>
</Grid>

我的问题是如何在单击“确定”后将小键盘的结果传回不同的文本字段?

这是键盘窗口背后的代码。到目前为止,我只有 1 号可以用于测试:

namespace WpfApplication2
{        
    public partial class QuantityKeypad : Window
    {
        bool btnClicked = false;
        string quantityResult = "";         

        public QuantityKeypad()
        {
            InitializeComponent();
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnZzero_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnOne_Click(object sender, RoutedEventArgs e)
        {
            if (btnClicked == true)
            {
                btnTotal.Text = "";
                btnClicked = false;
            }
            quantityResult = quantityResult += "1";
            btnTotal.Text = quantityResult;
        }

        private void btnTwo_Click(object sender, RoutedEventArgs e)
        {

        }       
    }
}

这是登录屏幕的代码:

namespace WpfApplication2
{    
    public partial class MainLogin2 : Window
    {
        public MainLogin2()
        {
            InitializeComponent();            
        }

        //OK button
        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
            this.Close();
        }

        //Cancel button - takes user back to login screen
        private void cancelButton_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //Removes text from login textboxes when clicked in
        //opens instance of Quantitykeypad when clicking inside textbox
        private void userIDTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            userIDTextBox.Text = "";
            QuantityKeypad loginKeypad = new QuantityKeypad();

            loginKeypad.Owner = this;
            loginKeypad.ShowDialog();
        }

        //Removes text from passcode textboxe when clicked in
        //opens instance of Quantitykeypad when clicking inside passcode textbox
        private void passcodeTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            passcodeTextBox.Text = "";
            QuantityKeypad loginKeypad = new QuantityKeypad();

            loginKeypad.Owner = this;
            loginKeypad.ShowDialog();
        }
    }
}

非常抱歉,如果这很简单,但我已经苦苦挣扎了好几天。

谢谢

最佳答案

您可以将私有(private)字段 quantityResult 设为可公开访问的属性:

public string QuantityResult { get; private set; }

这样,您就可以从中读取:

passcodeTextBox.Text = "";
QuantityKeypad loginKeypad = new QuantityKeypad();

loginKeypad.Owner = this;
loginKeypad.ShowDialog();

passcodeTextBox.Text = loginKeypad.QuantityResult;

关于c# - 创建我创建的窗口的新实例。多次调用做不同的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31760619/

相关文章:

c# - 发布网站时有没有办法排除整个文件夹?

c# - DataGridComboBoxColumn : how to set TextBinding

.net - 样式不适用于 Win7 x32 (.Net 4.0)

c# - WPF UIElements 中的动态 TextSize

python - PyQt 代码拆分——设计与功能

c# - 如果从数据库获取日期,如何获取日期前 3 天?

c# - 自动重置 MongoDb 服务

java - 在同一个类中创建一个类的引用变量是否正确?它与使用 new 关键字创建的普通对象有何不同?

python:与本地类名混淆

c# - WPF DataGrid 自动生成的列,更改标题名称