使用一个事件处理程序获取WPF按钮的Content值

时间:2019-05-26 13:23:50

标签: c# wpf wpf-controls

说,我有这个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>

1 个答案:

答案 0 :(得分: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());
    }