当用户在' whatsYourNameTextbox'中输入他/她的名字时并按下Enter键,我想要一个隐藏的文本块(' doYouWannaPlayTextblock')变为可见。这是我到目前为止在XAML中所得到的:
<TextBox x:Name="playerNameTextbox"
Grid.Column="1" Grid.Row="2"
VerticalAlignment="Top"
HorizontalAlignment="Center"
BorderBrush="Black"
Height="auto"
Width="160"
HorizontalContentAlignment="Center"
CaretBrush="red"
FontFamily="Segoe Print"
FontSize="30"
ToolTip="Type your name and then press Enter"
KeyUp="playerNameTextbox_KeyUp" />
<TextBlock Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2"
x:Name="doYouWannaPlayTextblock"
FontFamily="Segoe Print"
FontSize="16"
FontWeight="SemiBold"
Foreground="Red"
Padding="20"
Visibility="Hidden"
TextAlignment="Center"
TextWrapping="Wrap" Margin="0,0.4,0.2,-25">
Hi,
<TextBlock Text="{Binding Path=Text, ElementName=playerNameTextbox}"/>
- it's good to see you. Would you like to play 'Tables'? If so, just click the 'Choose Game' button.
</TextBlock>
我在Windows窗体中使用了以下内容:
private void playerOneNameTextbox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
doYouWantToPlayTextbox.Visible = true;
doYouWantToPlayTextbox.Text = "Hi, " + playerOneNameTextbox.Text + ". " +
"It's good to see you! " +
"\r\nWould you like to play with us? " +
"\r\nIf you would, just click right here in this box.";
clickToPlayTimer.Start();
}
但是当我尝试在WPF中的代码隐藏中使用它时,我无法超越以下内容:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void playerNameTextbox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
doYouWannaPlayTextblock.Visibility = Visible;
}
}
}
}
在哪里&#39;可见&#39;以及我尝试过的其他一切,都会引发一个红色的波浪形。
非常感谢任何帮助。
答案 0 :(得分:0)
private void playerNameTextbox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.doYouWannaPlayTextblock.Visibility = Visibility.Visible;
}
}
答案 1 :(得分:0)
首先,更改Visibility
的正确方法是:
doYouWannaPlayTextblock.Visibility = Visibility.Visible;
其次,您确定要使用KeyUp
吗?在这种情况下,KeyDown
会更有意义,因为只要您按Enter键就会触发,而不是在您释放它时触发。
第三,如果您的doYouWannaPlayTextblock在您按Enter键之前将不可见,则不需要在XAML中包含文本,因为文本将通过代码设置。添加文本的代码是
doYouWannaPlayTextblock.Text = "Hi, " + playerOneNameTextbox.Text + ". " +
"It's good to see you! " +
"\r\nWould you like to play with us? " +
"\r\nIf you would, just click right here in this box.";