How can I change my button click so that it is always executed automatically when my TextBox is no longer empty?

So I have a DataGrid in which I use MVVM to load my data into the DataGrid. Now my code looks like this that my button changes its content whenever I click on it and my TextBox is not empty. However, I want it to automatically when it detects my textBox is not empty it changes automatically because content and as soon as the TextBox is empty it goes back to the previous content. To do this, here is my xaml code:

<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}" HorizontalAlignment="Center" 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>

In addition, here is my Button Click event which should be executed automatically if my TextBox is not empty:

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


Read more here: https://stackoverflow.com/questions/67215522/how-can-i-change-my-button-click-so-that-it-is-always-executed-automatically-whe

Content Attribution

This content was originally published by Beginner.exe at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: