我正在研究一個沒有系結文本的搜索框解決方案,因為我使用該Text
屬性作為搜索命令的引數。那部分作業得很好。
現在我想TextBox
使用設定在TextBox
.
我嘗試了EventTrigger
,但是StoryBoard
塊Command
和 沒有將文本設定為零。
下面是我的代碼。任何想法?
<!--Search Box-->
<Grid Grid.Column="0">
<TextBox x:Name="FilterText">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding FilterDataCommand}" CommandParameter="{Binding ElementName=FilterText, Path=Text}"/>
</TextBox.InputBindings>
</TextBox>
<Button HorizontalAlignment="Right" Width="20" Height="20" Margin="5,0" Style="{StaticResource ButtonTransparentStyle}" Command="{Binding ClearSearchCommand}">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<StringAnimationUsingKeyFrames Storyboard.TargetName="FilterText" Storyboard.TargetProperty="Text">
<DiscreteStringKeyFrame KeyTime="0:0:1" Value=""/>
</StringAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
<Image Source="/VisionRanger;component/Images/Icons/icon.clear.dark.png" Stretch="UniformToFill" Margin="4,3,2,4" Opacity="0.5"/>
</Button>
</Grid>
<!--Search Button-->
<Button Grid.Column="1" Style="{StaticResource ButtonTransparentStyle}" Margin="5,0,0,0" Width="25" HorizontalAlignment="Left"
Command="{Binding FilterDataCommand}" CommandParameter="{Binding ElementName=FilterText, Path=Text}">
<Image Source="/VisionRanger;component/Images/Icons/icon.search.dark.png" Stretch="UniformToFill"/>
</Button>
uj5u.com熱心網友回復:
該Text
屬性 不可影片,請參閱檔案中的備注。在參考源中,FrameworkPropertyMetadata
被初始化 isAnimationProhibited
為true
。
一種解決方案是將MyFilterTextProperty
屬性系結到in模式的Text
屬性(這是默認設定)。TextBox
TwoWay
<TextBox Text="{Binding MyFilterTextProperty}"/>
然后在您的命令中ClearSearchCommand
,您可以清除MyFilterTextProperty
.
public void ExecuteClearSearchCommand(/*...parameter.*/)
{
MyFilterTextProperty = string.Empty;
// ...other code.
}
另一種方法是清除TextBox
使用自定義TriggerAction
. 安裝Microsoft.Xaml.Behaviors.Wpf NuGet 包。像這樣創建自定義觸發器操作。它公開了一個可以系結到Target
的依賴屬性。TextBox
如果發生事件,它將清除它。
public class ClearTextTriggerAction : TriggerAction<Button>
{
public static TextBox GetTarget(DependencyObject obj)
{
return (TextBox)obj.GetValue(TargetProperty);
}
public static void SetTarget(DependencyObject obj, int value)
{
obj.SetValue(TargetProperty, value);
}
public static readonly DependencyProperty TargetProperty = DependencyProperty.RegisterAttached(
"Target", typeof(TextBox), typeof(ClearTextTriggerAction), new PropertyMetadata(null));
protected override void Invoke(object parameter)
{
GetTarget(this)?.Clear();
}
}
在 XAML 中,您只需附加行為并系結FilterText
<Button HorizontalAlignment="Right" Width="20" Height="20" Margin="5,0" Style="{StaticResource ButtonTransparentStyle}" Command="{Binding ClearSearchCommand}">
<b:Interaction.Triggers>
<b:EventTrigger EventName="Click">
<local:ClearTextTriggerAction Target="{Binding ElementName=FilterText}"/>
</b:EventTrigger>
</b:Interaction.Triggers>
<Image Source="/VisionRanger;component/Images/Icons/icon.clear.dark.png" Stretch="UniformToFill" Margin="4,3,2,4" Opacity="0.5"/>
</Button>
b
必須包含 XAML bahviors的XML 命名空間。
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437697.html
上一篇:避免ListViewDelphi10.4.2中的空白項
下一篇:使文本框建議過濾器不區分大小寫