无法在WPF中的`TextBox`中输入字符串

时间:2016-10-21 03:13:03

标签: c# .net wpf xaml

我有两个窗户。单击MainWindow中的唯一一个按钮将显示第二个窗口PlayingGameWindow,其中包含TextBoxButton。但是,TextBoxButton都不能使用:前者无法输入,后者无法点击。他们都是灰色的。

enter image description here

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);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

你的第二个窗口中有IsEnabled = false。这会禁用所有输入。

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