简体   繁体   中英

Cannot input string in `TextBox` in WPF

I have two windows. Clicking the only one button in the MainWindow will show the second window PlayingGameWindow , which has a TextBox and a Button . However, neither the TextBox nor the Button can be used: the former cannot be input and the latter cannot be clicked. They're both gray.

在此处输入图片说明

MainWindow.xaml:

<Window x:Class="GuessFigure.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GuessFigure"
        mc:Ignorable="d"
        Title="猜数字" Height="350" Width="525">
    <Grid>
        <Button x:Name="button" Content="开始游戏" HorizontalAlignment="Left" Margin="228,139,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

    </Grid>
</Window>

MainWindow.cs:

namespace GuessFigure
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //开始游戏按钮
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            PlayingGameWindow playingGameWindow = new PlayingGameWindow();

            playingGameWindow.Show();
            Close();
        }
    }
}

PlayingGameWindow.xaml:

<Window x:Class="GuessFigure.PlayingGameWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GuessFigure"
        mc:Ignorable="d"
        Title="PlayingGame" Height="300" Width="300">
    <Grid
        xmlns:viewmodel="clr-namespace:GuessFigure.ViewModel" IsEnabled="False">
        <Grid.Resources>
            <viewmodel:PlayGameViewModel x:Key="playGameViewModel"/>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="40*"/>
            <RowDefinition Height="51*"/>
            <RowDefinition Height="81*"/>
            <RowDefinition Height="40*"/>
            <RowDefinition Height="58*"/>
        </Grid.RowDefinitions>
        <Grid.DataContext>
            <Binding Source="{StaticResource playGameViewModel}" />
        </Grid.DataContext>
        <TextBlock x:Name="tbTime" HorizontalAlignment="Left" Margin="76,24,0,0" TextWrapping="Wrap" Text="{Binding Path=TimeText}" VerticalAlignment="Top" Grid.Row="4" Height="15" Width="88"/>
        <TextBox x:Name="answerTextBox" HorizontalAlignment="Left" Height="23" Margin="76,37.8,0,0" TextWrapping="Wrap" Text="{Binding Path=UserAnswer}" VerticalAlignment="Top" Width="120" Grid.Row="2"/>
        <TextBlock x:Name="roundNumber" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Path=RoundName}" PreviewTextInput="NumberValidationTextBox" Height="15"/>
        <Button x:Name="button" Content="确认" HorizontalAlignment="Left" Margin="176,11,0,0" Grid.Row="3" VerticalAlignment="Top" Width="75" IsEnabled="True"/>
        <TextBlock x:Name="currentQuestiontextBlock" HorizontalAlignment="Left" Margin="94,26,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Path=Question}" VerticalAlignment="Top"/>

    </Grid>
</Window>

PlayingGameWIndow.cs:

namespace GuessFigure
{
    /// <summary>
    /// PlayingGame.xaml 的交互逻辑
    /// </summary>
    public partial class PlayingGameWindow : Window
    {
        public PlayingGameWindow()
        {
            InitializeComponent();
        }


        private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled
                = regex.IsMatch(e.Text);
        }
    }
}

You have IsEnabled=false in your second window. That disables all your inputs.

<Grid
        xmlns:viewmodel="clr-namespace:GuessFigure.ViewModel" IsEnabled="False">

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.

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