更改组合框选择时清除文本框
Clear TextBox when ComboBox Selection is Changed
下面是我的TextBox
<TextBox Text="Hello" />
和我的ComboBox
<ComboBox VerticalAlignment="Center" Name="srchType" SelectedIndex="0" SelectionChanged="srchType_SelectionChanged">
<ComboBoxItem Content="Question" />
<ComboBoxItem Content="Value" />
</ComboBox>
现在我可以使用隐藏代码清除 TextBox 中的文本。如下图
private void srchType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
asb.Text = "";
}
但我想在 xaml
中执行此操作而不是使用 CodeBehind,因为我的组合框很少,并且每个选择更改事件都必须执行特定操作。 (简单的操作,但它们与清除文本框不同)。
任何人都可以告诉我如何在 xaml
中实现这一点。或者这甚至可能吗?
您可以使用 XAML Behaviors
:
声明所需的命名空间:
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
并使用它
<StackPanel>
<TextBox x:Name="myTextBox"
Text="Hello"/>
<ComboBox SelectedIndex="0">
<i:Interaction.Behaviors>
<i:BehaviorCollection>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:ChangePropertyAction PropertyName="Text"
TargetObject="{Binding ElementName=myTextBox}"
Value="{Binding Empty, Source={StaticResource EmptyString}}">
</core:ChangePropertyAction>
</core:EventTriggerBehavior>
</i:BehaviorCollection>
</i:Interaction.Behaviors>
<x:Int32>1</x:Int32>
<x:Int32>2</x:Int32>
<x:Int32>3</x:Int32>
<x:Int32>4</x:Int32>
</ComboBox>
</StackPanel>
但是,你可以看到
Value="{Binding Empty, Source={StaticResource EmptyString}}"
默认情况下,您不能在 XAML 中声明空字符串(也许在 UWP 中发生了一些变化),因此我为其创建了帮助程序 class:
public class EmptyString : DependencyObject
{
public string Empty
{
get { return (string)GetValue(EmptyProperty); }
}
public static readonly DependencyProperty EmptyProperty =
DependencyProperty.Register("Empty", typeof(string), typeof(EmptyString), new PropertyMetadata(string.Empty));
}
并在页面上声明:
<Page.Resources>
<local:EmptyString x:Name="EmptyString"/>
</Page.Resources>
答案与 Andrii 发布的非常接近,但在 UWP 中存在一些差异。
首先,Behaviors SDK不是UWP内置的,需要从NuGet单独下载
您可以使用以下命令安装它:
Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed
或者只使用 NuGet 包管理器并搜索 Microsoft.Xaml.Behaviors.Uwp.Managed
。
安装后,您只需将 XAML using 语句添加到页面顶部即可:
<Page ...
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core" />
并在需要时使用ChangePropertyAction
更改值。在 UWP XAML 中,您实际上通常可以使用空字符串作为值,并且它按预期工作。
<ComboBox VerticalAlignment="Center" Name="srchType" SelectedIndex="0">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:ChangePropertyAction
TargetObject="{Binding ElementName=TheTextBoxToUpdate}"
PropertyName="Text" Value="" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<ComboBoxItem Content="Question" />
<ComboBoxItem Content="Value" />
</ComboBox>
下面是我的TextBox
<TextBox Text="Hello" />
和我的ComboBox
<ComboBox VerticalAlignment="Center" Name="srchType" SelectedIndex="0" SelectionChanged="srchType_SelectionChanged">
<ComboBoxItem Content="Question" />
<ComboBoxItem Content="Value" />
</ComboBox>
现在我可以使用隐藏代码清除 TextBox 中的文本。如下图
private void srchType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
asb.Text = "";
}
但我想在 xaml
中执行此操作而不是使用 CodeBehind,因为我的组合框很少,并且每个选择更改事件都必须执行特定操作。 (简单的操作,但它们与清除文本框不同)。
任何人都可以告诉我如何在 xaml
中实现这一点。或者这甚至可能吗?
您可以使用 XAML Behaviors
:
声明所需的命名空间:
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
并使用它
<StackPanel>
<TextBox x:Name="myTextBox"
Text="Hello"/>
<ComboBox SelectedIndex="0">
<i:Interaction.Behaviors>
<i:BehaviorCollection>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:ChangePropertyAction PropertyName="Text"
TargetObject="{Binding ElementName=myTextBox}"
Value="{Binding Empty, Source={StaticResource EmptyString}}">
</core:ChangePropertyAction>
</core:EventTriggerBehavior>
</i:BehaviorCollection>
</i:Interaction.Behaviors>
<x:Int32>1</x:Int32>
<x:Int32>2</x:Int32>
<x:Int32>3</x:Int32>
<x:Int32>4</x:Int32>
</ComboBox>
</StackPanel>
但是,你可以看到
Value="{Binding Empty, Source={StaticResource EmptyString}}"
默认情况下,您不能在 XAML 中声明空字符串(也许在 UWP 中发生了一些变化),因此我为其创建了帮助程序 class:
public class EmptyString : DependencyObject
{
public string Empty
{
get { return (string)GetValue(EmptyProperty); }
}
public static readonly DependencyProperty EmptyProperty =
DependencyProperty.Register("Empty", typeof(string), typeof(EmptyString), new PropertyMetadata(string.Empty));
}
并在页面上声明:
<Page.Resources>
<local:EmptyString x:Name="EmptyString"/>
</Page.Resources>
答案与 Andrii 发布的非常接近,但在 UWP 中存在一些差异。
首先,Behaviors SDK不是UWP内置的,需要从NuGet单独下载
您可以使用以下命令安装它:
Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed
或者只使用 NuGet 包管理器并搜索 Microsoft.Xaml.Behaviors.Uwp.Managed
。
安装后,您只需将 XAML using 语句添加到页面顶部即可:
<Page ...
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core" />
并在需要时使用ChangePropertyAction
更改值。在 UWP XAML 中,您实际上通常可以使用空字符串作为值,并且它按预期工作。
<ComboBox VerticalAlignment="Center" Name="srchType" SelectedIndex="0">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:ChangePropertyAction
TargetObject="{Binding ElementName=TheTextBoxToUpdate}"
PropertyName="Text" Value="" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<ComboBoxItem Content="Question" />
<ComboBoxItem Content="Value" />
</ComboBox>