簡體   English   中英

無法在WPF的`TextBox`中輸入字符串

[英]Cannot input string in `TextBox` in WPF

我有兩個窗戶。 單擊MainWindow的唯一一個按鈕將顯示第二個窗口PlayingGameWindow ,該窗口具有一個TextBox和一個Button 但是,不能使用TextBoxButton :前者無法輸入,后者無法單擊。 他們都是灰色的。

在此處輸入圖片說明

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>

玩GameWIndow.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);
        }
    }
}

在第二個窗口中,您具有IsEnabled = false。 這將禁用您的所有輸入。

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

暫無
暫無

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

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