Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过XAML中的复选框更改文本框文本和焦点_C#_Wpf_Xaml - Fatal编程技术网

C# 通过XAML中的复选框更改文本框文本和焦点

C# 通过XAML中的复选框更改文本框文本和焦点,c#,wpf,xaml,C#,Wpf,Xaml,当我选中或取消选中复选框时,我想清除文本框并设置焦点。我使用代码隐藏实现了它 我的圣诞礼物: <CheckBox x:Name="checkbox1" Checked="checkbox1_Checked" Unchecked="checkbox1_Checked" /> <TextBox x:Name="textbox1" Text="Hello" /> 是否可以在XAML中完成所有操作?使用数据触发器。它们通过创建到常规属性的绑定来工作,然后 监视更改 例如,考虑下

当我选中或取消选中
复选框时,我想清除
文本框并设置焦点。我使用代码隐藏实现了它

我的圣诞礼物:

<CheckBox x:Name="checkbox1" Checked="checkbox1_Checked" Unchecked="checkbox1_Checked" />
<TextBox x:Name="textbox1" Text="Hello" />

是否可以在XAML中完成所有操作?

使用数据触发器。它们通过创建到常规属性的绑定来工作,然后 监视更改

例如,考虑下面的例子:

<Window x:Class="WpfTutorialSamples.Styles.StyleDataTriggerSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="StyleDataTriggerSample" Height="200" Width="200">

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <CheckBox Name="cbSample" Content="Hello, world?" />
        <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48">

            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Text" Value="No" />
                    <Setter Property="Foreground" Value="Red" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">
                            <Setter Property="Text" Value="Yes!" />
                            <Setter Property="Foreground" Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>

        </TextBlock>
    </StackPanel>
</Window>

在本例中,我们有一个复选框和一个文本块。使用DataTrigger,我们绑定TextBlock 到复选框的IsChecked属性。然后,我们提供一个默认样式,其中 是“否”,前景色是红色,然后,使用DataTrigger,我们为 当复选框的IsChecked属性更改为True时,在这种情况下,我们将其设置为True 绿色,文本为“是!”(如屏幕截图所示)


您可以在中找到更多信息。

使用数据触发器。它们通过创建到常规属性的绑定来工作,然后 监视更改

例如,考虑下面的例子:

<Window x:Class="WpfTutorialSamples.Styles.StyleDataTriggerSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="StyleDataTriggerSample" Height="200" Width="200">

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <CheckBox Name="cbSample" Content="Hello, world?" />
        <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48">

            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Text" Value="No" />
                    <Setter Property="Foreground" Value="Red" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">
                            <Setter Property="Text" Value="Yes!" />
                            <Setter Property="Foreground" Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>

        </TextBlock>
    </StackPanel>
</Window>

在本例中,我们有一个复选框和一个文本块。使用DataTrigger,我们绑定TextBlock 到复选框的IsChecked属性。然后,我们提供一个默认样式,其中 是“否”,前景色是红色,然后,使用DataTrigger,我们为 当复选框的IsChecked属性更改为True时,在这种情况下,我们将其设置为True 绿色,文本为“是!”(如屏幕截图所示)

您可以在中找到更多信息