WPF---Xaml中改变ViewModel的值

在开发中遇到实现如下需求的情景:一个输入框,旁边一个清空输入的按钮,当输入框中有内容时显示清空按钮,点击该按钮可以清空输入框内容,当输入框中无内容时隐藏按钮

 

当然这个需求使用wpf的绑定功能很容易实现

 <TextBox Width="220"
                         Height="32"
                         HorizontalAlignment="Right"
                         HorizontalContentAlignment="Left"
                         VerticalContentAlignment="Center"
                         MaxLength="20"
                         Text="{Binding SearchContent,
                                        UpdateSourceTrigger=PropertyChanged}"
                         pt:WatermarkHelper.WatermarkContent="{lex:LocText Search}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="TextChanged">
                            <i:InvokeCommandAction Command="{Binding TextChangedCommand}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
 </TextBox>
                <pt:IconLabelButton Width="32"
                                    Margin="-32,0,32,0"
                                    Command="{Binding ClearCommand}"
                                    Icon="/Resource;component/res/GeneralClear.png"
                                    Visibility="{Binding IsShowClearButton,
                                                         Converter={StaticResource VisiblityConverter}}" />

 

   public  ICommand      TextChangedCommand = new DelegateCommand<string>(OnTextChangedCommand);  
   public  ICommand      ClearCommand = new DelegateCommand(OnClearCommand);

private void OnTextChangedCommand(string obj)
        {
            if (string.IsNullOrEmpty(SearchContent))
            {
                IsShowClearButton = false;
                return;
            }

            if (SearchContent.Length > 0)
            {
                IsShowClearButton = true;
            }
            else
            {
                IsShowClearButton = false;
            }
        }

   private void OnClearCommand()
        {
            SearchContent = string.Empty;
     
        }

上面思路是通过Textbox的TextChanged事情来处理按钮的显示隐藏。

 

有没更简单的方案,只在xaml中就实现这个需求,毕竟这个跟业务逻辑完全没关系,只是界面上的变化的东西。

经过努力终于找到方案了,下面看实现方法:需要引用 System.Windows.Interactivity“ 和 ”Microsoft.Expression.Interactions”程序集



<
TextBox Width="300" Name="tbSearch" Height="30" Style="{DynamicResource TextBoxStyle}" pt:WatermarkHelper.WatermarkContent="{lex:LocText Search}" Text="{Binding SearchText}"> </TextBox> <pt:IconLabelButton Width="32" x:Name="btnClear" Margin="-32,0,0,0" Icon="/Resource;component/res/GeneralClear.png"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <ei:ChangePropertyAction TargetObject="{Binding}" PropertyName="SearchText" Value="" /> </i:EventTrigger> </i:Interaction.Triggers> <pt:IconLabelButton.Style> <Style BasedOn="{StaticResource IconLabelButtonStyle}" TargetType="{x:Type pt:IconLabelButton}"> <Style.Triggers> <DataTrigger Binding="{ Binding ElementName=tbSearch, Path=Text}" Value=""> <Setter Property="Control.Visibility" Value="Hidden" /> </DataTrigger> </Style.Triggers> </Style> </pt:IconLabelButton.Style> </pt:IconLabelButton>

button控件的显示隐藏通过DataTrigger来实现,通过检测到Textbox的Text属性为空值时,设置属性隐藏。

点击按钮时通过EventTrigger的 ChangePropertyAction   实现, TargetOject绑定到ViewModel, PropertyName设置为TextBox的绑定ViewModel属性,直接改变绑定的属性值实现清空textbox值。

(PS通过ChangePropertyAction 的TargetOject绑定控件, 清空Text属性,可以清空textbox的界面值,但是无法同步textbox的viewmodel绑定值)

 

 

       只有敢于尝试不同方法才可以进步哟,希望这篇文章对大家有帮助

 

转载于:https://www.cnblogs.com/karl-F/p/7267174.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF MVVM 架构,可以使用 InvokeCommandAction 来触发 ViewModel 的命令,并且可以传递一个参数。如果需要传递多个参数,可以使用以下方法: 1. 使用命令参数对象 定义一个类,包含需要传递的多个参数,例如: ``` public class CommandParameter { public string Parameter1 { get; set; } public int Parameter2 { get; set; } } ``` 在 XAML ,使用该类作为 InvokeCommandAction 的 CommandParameter 属性的: ``` <i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding CommandParameter}" /> ``` 在 ViewModel ,命令的 Execute 方法可以接收该类的实例作为参数: ``` public RelayCommand<CommandParameter> MyCommand { get; set; } public void MyCommandExecute(CommandParameter parameter) { // 使用参数 } ``` 2. 使用触发事件的参数 在 XAML ,可以使用 EventTrigger 触发一个事件,并且可以使用 EventTrigger 的 EventArgsConverter 属性将事件参数转换为需要的类型。例如: ``` <i:Interaction.Triggers> <i:EventTrigger EventName="MyEvent"> <i:InvokeCommandAction Command="{Binding MyCommand}"> <i:InvokeCommandAction.CommandParameter> <MultiBinding Converter="{StaticResource MyConverter}"> <Binding Path="Parameter1" /> <Binding Path="Parameter2" /> </MultiBinding> </i:InvokeCommandAction.CommandParameter> </i:InvokeCommandAction> </i:EventTrigger> </i:Interaction.Triggers> ``` 在这里,使用 MultiBinding 将多个绑定传递给一个转换器。转换器将这些转换为需要的类型,并且将它们封装到一个对象,然后作为命令的参数传递给 ViewModel。 在 ViewModel ,命令的 Execute 方法可以接收该对象作为参数: ``` public RelayCommand<MyParameter> MyCommand { get; set; } public void MyCommandExecute(MyParameter parameter) { // 使用参数 } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值