私はWPFを推奨ブックSams Teach Yourself WPF in 24 Hoursで学習しています。私はRoutedEvents
を扱っている「時間9」にいる。以下のコードは私が本からタイプしたものです。私が理解しているように、ButtonBase.Click
は、ボタンが呼び出している添付イベントです。これは私が期待しているように機能します。添付されたイベントが予期したとおりに動作しない
<StackPanel ButtonBase.Click="StackPanel_Click">
<Button Content="Red" Foreground="Red"/>
<Button Content="Green" Foreground="Green"/>
<Button Content="Blue" Foreground="Blue"/>
<TextBlock x:Name="Output"
HorizontalAlignment="Center"
Text="What color will you choose?"/>
</StackPanel>
private void StackPanel_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)e.Source;
Output.Text=string.Format(
$"You choose the color {button.Content}"
);
Output.Background = button.Foreground;
}
私はTextBox
失っが集中したときにMessageBox
ディスプレイを持ってしようとする試みには、以下の見ているものにコードを変更します。メッセージボックスは、Button
がプッシュされたときにも発生します。誰かが理解できない部分を明確にするのに役立つことができますか?
<StackPanel ButtonBase.Click="StackPanel_Click"
TextBoxBase.LostFocus="StackPanel_LostFocus">
<Button Content="Red" Foreground="Red"/>
<Button Content="Green" Foreground="Green"/>
<Button Content="Blue" Foreground="Blue"/>
<TextBox HorizontalAlignment="Center">Here's some text.</TextBox>
<TextBlock x:Name="Output"
HorizontalAlignment="Center"
Text="What color will you choose?"/>
</StackPanel>
private void StackPanel_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)e.Source;
Output.Text=string.Format(
$"You choose the color {button.Content}"
);
Output.Background = button.Foreground;
}
private void StackPanel_LostFocus(object sender, RoutedEventArgs e)
{
MessageBox.Show("The text box has lost focus.");
}
あなたは両方ので、両方がそれをトリガーしますしている: 'ButtonBase.Clickは=「StackPanel_Click」 TextBoxBase.LostFocus =「StackPanel_LostFocus」>' – CodingYoshi