【问题标题】:Get the Content value of the WPF button using one event handler [duplicate]使用一个事件处理程序获取 WPF 按钮的内容值 [重复]
【发布时间】:2019-05-26 13:23:49
【问题描述】:

说,我有这个 WPF 网格。如何使用一个事件处理程序获取 WPF 按钮的 Content 值?

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button Content="A" Grid.Row="0" />
    <Button Content="B" Grid.Row="0" Grid.Column="1" />        
    <Button Content="C" Grid.Row="0" Grid.Column="2" />        
    <Button Content="D" Grid.Row="0" Grid.Column="3" />
    <Button Content="E" Grid.Row="1" />
    <Button Content="F" Grid.Row="1" Grid.Column="1" />
    <Button Content="G" Grid.Row="1" Grid.Column="2" />
    <Button Content="H" Grid.Row="1" Grid.Column="3" />
    <Button Content="I" Grid.Row="2" />
    <Button Content="B" Grid.Row="2" Grid.Column="1" />
    <Button Content="J" Grid.Row="2" Grid.Column="2" />
    <Button Content="K" Grid.Row="2" Grid.Column="3" />
</Grid>

【问题讨论】:

  • 你想获取特定按钮或所有子按钮的内容值?
  • 如果我点击按钮A,我会得到值“A”等等。
  • @检查答案!

标签: c# wpf wpf-controls


【解决方案1】:

Xaml

 <Button Content="abc" Click="ButtonBase_OnClick"></Button>

事件

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var senderBtn = sender as Button;
        MessageBox.Show(senderBtn.Content.ToString());
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2021-05-16
    • 2018-06-27
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多