正确使用 PropertyChangedTrigger 和 ChangePropertyAction [英] Proper use of PropertyChangedTrigger and ChangePropertyAction

查看:16
本文介绍了正确使用 PropertyChangedTrigger 和 ChangePropertyAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的 ComboBox 上的 ItemsSource 属性发生变化时,我正在尝试设置默认选定值

I'm trying to set a default selected value when ItemsSource property changes on my ComboBox

我的xaml:

<ComboBox ItemsSource="{Binding SelectedItemsSource, Mode=OneWay}" x:Name="c1">
   <i:Interaction.Triggers>
         <ei:PropertyChangedTrigger Binding="{Binding ItemsSource,RelativeSource={RelativeSource Self}}">
              <ei:ChangePropertyAction PropertyName="SelectedIndex" Value="{StaticResource zero}" TargetName="c1"/>
         </ei:PropertyChangedTrigger>                        
   </i:Interaction.Triggers>             
</ComboBox>

我的 VM 中的 SelectedItemsSource 动态变化,我希望每次发生这种情况时都选择第一个项目.

The SelectedItemsSource in my VM changes dynamically and I want to have the first item selected each time this happens .

知道为什么这不起作用吗?

Any idea why this doesn't work?

推荐答案

尝试设置属性 IsSynchronizedWithCurrentItemtrue 为您的组合框并完全删除触发器 -

Try setting property IsSynchronizedWithCurrentItem to true for your combobox and remove the trigger completely -

<ComboBox ItemsSource="{Binding SelectedItemsSource, Mode=OneWay}"
          IsSynchronizedWithCurrentItem="True">             
</ComboBox>

这篇关于正确使用 PropertyChangedTrigger 和 ChangePropertyAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆