为什么我不能向 DataGrid 中的按钮添加单击事件?

Why can't I add a click event to my button in the DataGrid?

所以我在WPF中创建了一个DataGrid,并在我的DataGrid中定义了这样一个按钮(按钮的名字是Clear Button):

<Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"
                TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Gray"/>
                <Setter Property="BorderBrush" Value="White"/>
                <Setter Property="BorderThickness" Value="0 0 1 0"/>
                <Setter Property="Content">
                    <Setter.Value>
                        <StackPanel Background="Gray" Height="40" Width="11">
                            <Separator Background="White" Margin="0,17,-1,0"/>
                            <Button x:Name="ClearButton" Background="Transparent" BorderThickness="0" FontFamily="Segoe MDL2 Assets" FontWeight="Light" FontSize="7" Content="&#xE71C;" Height="13"  Margin="0 5 0 0" Click="ClearButton_Click"/>
                        </StackPanel>
                    </Setter.Value>
                </Setter>
            </Style>

现在我想给这个按钮添加一个点击事件,但是当我想启动程序时它对我说如下:

"ClearButton_Click" 无效。 “点击”不是“System.Windows.Controls.DataGridTextColumn”的事件。.

那么为什么我不能使用 Click 事件,我怎样才能让它工作?

编辑: 我的完整 Xaml 代码如下所示:

Window.DataContext>
    <local:PersonsViewModel />
</Window.DataContext>

<StackPanel>
    <DataGrid HorizontalGridLinesBrush="Gray" VerticalGridLinesBrush="Gray"  ItemsSource="{Binding Persons}" AutoGenerateColumns="False" Height="309"  Margin="110,76,58,0" x:Name="gridd" >
        <DataGrid.Resources>

            <TextBlock Text="&#xE71C;" x:Key="DataGridSelectAllButtonStyle.Content"/>
            <Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"
                TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Gray"/>
                <Setter Property="BorderBrush" Value="White"/>
                <Setter Property="BorderThickness" Value="0 0 1 0"/>
                <EventSetter Event="Click" Handler="ClearButton_Click"/>
                <Setter Property="Content">
                    <Setter.Value>
                        <StackPanel Background="Gray" Height="40" Width="11">
                            <Separator Background="White" Margin="0,17,-1,0"/>
                            <TextBlock Background="{x:Null}" Height="13"  Margin="0 5 0 0"
                        FontFamily="Segoe MDL2 Assets" FontWeight="Light" FontSize="7"
                        Text="{Binding Text, Source={StaticResource DataGridSelectAllButtonStyle.Content}}">
                            </TextBlock>
                        </StackPanel>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Background" Value="Gray"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="BorderBrush" Value="White"/>
                <Setter Property="BorderThickness" Value="0,0,1,0" />
            </Style>

            <Style TargetType="{x:Type DataGridCell}">

            </Style>

            <Style TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Width" Value="15"/>
                <Setter Property="Height" Value="20"/>
                <Setter Property="Background" Value="White"/>
                <Setter Property="BorderBrush" Value="Gray"/>
                <Setter Property="BorderThickness" Value="1"/>
            </Style>
        </DataGrid.Resources>

        <DataGrid.Columns>

            <DataGridTextColumn Width="*" SortMemberPath="Id" Binding="{Binding Id}" CanUserResize="False" IsReadOnly="True">
                <DataGridTextColumn.Header>
                    <StackPanel>
                        <TextBlock Text="Id" Margin="3 0 0 0" Width="148" Height="17"/>
                        <Separator Background="White" Width="125" />
                        <TextBox x:Name="IDSearcBox" Width="113" Height="19" Margin="0 0 0 2" 
                                 Text="{Binding DataContext.QueryforID, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}" />
                    </StackPanel>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>

            <DataGridTextColumn Width="*" SortMemberPath="Id" Binding="{Binding Name}" CanUserResize="False" IsReadOnly="True">
                <DataGridTextColumn.Header>
                    <StackPanel>
                        <TextBlock Text="Name" Margin="3 0 0 0" Width="148" Height="17"/>
                        <Separator  Background="White" Width="125" />
                        <TextBox x:Name="Name" Width="113" Height="19" Margin="0 0 0 2" 
                                 Text="{Binding DataContext.Queryforname, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}"/>
                    </StackPanel>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>

            <DataGridTextColumn Width="*" SortMemberPath="Id" Binding="{Binding Country}" CanUserResize="False" IsReadOnly="True">
                <DataGridTextColumn.Header>
                    <StackPanel>
                        <TextBlock Text="Land" Margin="3 0 0 0" Width="148" Height="17"/>
                        <Separator Background="White" Width="125"/>
                        <TextBox x:Name="Birthday" Width="113" Height="19" Margin="0 0 0 2"  
                                 Text="{Binding DataContext.QueryforCountry, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}" />
                    </StackPanel>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>


            <DataGridTextColumn Width="*" SortMemberPath="Id" Binding="{Binding Location}" CanUserResize="False" IsReadOnly="True">
                <DataGridTextColumn.Header>
                    <StackPanel>
                        <TextBlock Text="Ort" Margin="3 0 0 0" Width="148" Height="17"/>
                        <Separator Background="White" Width="125" />
                        <TextBox x:Name="Ort" Width="113" Height="19" Margin="0 0 0 2"   
                                Text="{Binding DataContext.QueryforLocation, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}"/>
                    </StackPanel>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>


            <DataGridTextColumn SortMemberPath="Id" Binding="{Binding Age}" CanUserResize="False" IsReadOnly="True">
                <DataGridTextColumn.Header>
                    <StackPanel>
                        <TextBlock Text="Alter" Margin="0 0 0 0" Width="115" Height="17"/>
                        <Separator Background="White" Width="125"/>
                        <TextBox x:Name="alter" Width="119" Height="19" Margin="0 0 0 2" 
                                 Text="{Binding DataContext.QueryforAge, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}"/>
                    </StackPanel>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>


        </DataGrid.Columns>

    </DataGrid>
</StackPanel>

我更改按钮内容的方法:

 private void ClearButton_Click(object sender, RoutedEventArgs e)
    {
        TextBlock textBlock = (TextBlock)Resources["DataGridSelectAllButtonStyle.Content"];
        textBlock.Text = textBlock.Text == "\xE711"
            ? "\xE71C"
            : "\xE711";

        if (!String.IsNullOrEmpty(IDSearcBox.Text))
        {
            IDSearcBox.Clear();
        }
    }

前提是样式在使用它的地方和处理程序所在的相同 class 中声明 - 试试这个:

    <Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"
            TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Gray"/>
        <Setter Property="BorderBrush" Value="White"/>
        <Setter Property="BorderThickness" Value="0 0 1 0"/>
        <Setter Property="Content">
            <Setter.Value>
                <StackPanel Background="Gray" Height="40" Width="11">
                    <Separator Background="White" Margin="0,17,-1,0"/>
                    <Button x:Name="ClearButton" Background="Transparent" BorderThickness="0"
                            FontFamily="Segoe MDL2 Assets" FontWeight="Light" FontSize="7"
                            Content="&#xE71C;" Height="13"  Margin="0 5 0 0">
                        <Button.Style>
                            <Style TargetType="Button">
                                <EventSetter Event="Click" Handler="ClearButton_Click"/>
                            </Style>
                        </Button.Style>
                    </Button>
                </StackPanel>
            </Setter.Value>
        </Setter>
    </Style>

My goal is that when my text boxes are filled, the button then changes to a cross as the content

您可以这样更改样式:

<Window.Resources>
    <TextBlock Text="&#xE71C;" x:Key="DataGridSelectAllButtonStyle.Content"/>
    <Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"
            TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Gray"/>
        <Setter Property="BorderBrush" Value="White"/>
        <Setter Property="BorderThickness" Value="0 0 1 0"/>
        <EventSetter Event="Click" Handler="ClearButton_Click"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Background="Gray" Height="40" Width="11">
                        <Separator Background="White" Margin="0,17,-1,0"/>
                        <TextBlock Background="{x:Null}" Height="13"  Margin="0 5 0 0"
                            FontFamily="Segoe MDL2 Assets" FontWeight="Light" FontSize="7"
                            Text="{Binding Text, Source={StaticResource DataGridSelectAllButtonStyle.Content}}">
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<StackPanel Height="600" Width="800">
    <DataGrid ItemsSource="{x:Static local:Myclass.MyModel}"
            RowHeaderWidth="40">
    </DataGrid>
    <Button Click="Button_Click" Content="Change"/>
</StackPanel>

通过设置“DataGridSelectAllButtonStyle.Content”资源的Text属性,还可以改变按钮样式中TextBlock的内容。

在Window方法中,需要更改内容的地方,从资源中获取TextBlock并为其赋值:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ClearButton_Click(object sender, RoutedEventArgs e)
    {

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        TextBlock textBlock = (TextBlock)Resources["DataGridSelectAllButtonStyle.Content"];
        textBlock.Text = textBlock.Text == "\xE711"
            ? "\xE71C"
            : "\xE711";
    }
}

如果使用的资源不属于 Window,而是属于嵌套元素,则必须使用 FindResource 方法从事件源的元素开始搜索它们:

    private void ClearButton_Click(object sender, RoutedEventArgs e)
    {
        if (sender is FrameworkElement element)
        {
            TextBlock textBlock = (TextBlock)element.FindResource("DataGridSelectAllButtonStyle.Content");
            textBlock.Text = textBlock.Text == "\xE711"
                ? "\xE71C"
                : "\xE711";
        }

    }