wpf 使用目标名称触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12607148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trigger with target name
提问by AustinTX
I have a couple text boxes and collection of radio buttons in WPF. I want to use the trigger to set the IsChecked property of oly one radio button which has a name if any text boxes get focus. I check a few examples but I could not find what I looking for. Remember, we are using MVVM pattern and no code behind.
我在 WPF 中有几个文本框和单选按钮集合。我想使用触发器来设置一个单选按钮的 IsChecked 属性,如果任何文本框获得焦点,该按钮具有名称。我检查了几个例子,但我找不到我要找的东西。请记住,我们使用的是 MVVM 模式并且没有背后的代码。
I tried the following codes and have this compile error:
我尝试了以下代码并出现此编译错误:
TargetName property cannot be set on a Style Setter
无法在样式设置器上设置 TargetName 属性
<UserControl.Resources>
<Style x:Name="myTest" TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="RadioButton.IsChecked" Value="True" TargetName="myRadioButton"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
I read other posts and DataTrigger fix the problem.
我阅读了其他帖子,DataTrigger 解决了这个问题。
<Style x:Name="myTest2" TargetType="RadioButton" >
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=myTextBox}" Value="True">
<Setter Property="IsChecked" Value="True" ></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
回答by Pale Ale
Create a ControlTemplate and add your trigger to ControlTemplate.Triggers
创建一个 ControlTemplate 并将您的触发器添加到 ControlTemplate.Triggers
<ControlTemplate.Triggers>
<Trigger Property="HasText" Value="True">
<Setter Property="Visibility" TargetName="LabelText" Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
回答by Praboda
Write styles for each RadioButton providing respective TextBox as triggering element. Following is an example for 3 TextBoxes & 3 RadioButtons.
为每个提供各自 TextBox 作为触发元素的 RadioButton 编写样式。以下是 3 个文本框和 3 个单选按钮的示例。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox x:Name="txtBox0" Grid.Row="0"/>
<TextBox x:Name="txtBox1" Grid.Row="1"/>
<TextBox x:Name="txtBox2" Grid.Row="2"/>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<RadioButton GroupName="grp1" Content="txt1">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=txtBox0}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
<RadioButton GroupName="grp1" Content="txt2">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=txtBox1}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
<RadioButton GroupName="grp1" Content="txt3">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=txtBox2}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
</StackPanel>
</Grid>
回答by decaprime
I think you're looking for the GotFocus Event.
我认为您正在寻找 GotFocus 活动。
In XAML:
在 XAML 中:
<TextBox x:Name="textBox1" GotFocus="tb_GotFocus"/>
<TextBox x:Name="textBox2" GotFocus="tb_GotFocus"/>
<TextBox x:Name="textBox3" GotFocus="tb_GotFocus"/>
<RadioButton x:Name="myRadioButton"/>
Then in your C# your event handler could look something like this
然后在您的 C# 中,您的事件处理程序可能如下所示
private void tb_GotFocus(object sender, RoutedEventArgs e)
{
myRadioButton.IsChecked = true;
}
If any of the TextBoxes gets focused it will check the RadioButton named myRadioButton.
如果任何 TextBoxes 获得焦点,它将检查名为 myRadioButton 的 RadioButton。
回答by Tejas Sharma
From MSDN:
来自 MSDN:
You can set this property to the name of any element within the scope of where the setter collection (the collection that this setter is part of) is applied. This is typically a named element that is within the template that contains this setter.
您可以将此属性设置为应用 setter 集合(此 setter 所属的集合)范围内的任何元素的名称。这通常是包含此 setter 的模板中的命名元素。
TargetNameis mostly used within control templates and not simply within styles like you are attempting to use it. What you can do is to bind your RadioButton's IsCheckedDP to the IsMouseOverDP of the TextBox.
TargetName主要在控件模板中使用,而不仅仅是在您尝试使用它的样式中使用。你可以做的是绑定你RadioButton的IsCheckedDP到IsMouseOver的DP TextBox。

