wpf 正确使用 PropertyChangedTrigger 和 ChangePropertyAction

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12337442/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:16:52  来源:igfitidea点击:

Proper use of PropertyChangedTrigger and ChangePropertyAction

wpfxaml

提问by eran otzap

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

ItemsSource我的属性发生变化时,我试图设置一个默认的选定值ComboBox

My xaml :

我的 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>

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

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

Any idea why this doesn't work?

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

回答by Rohit Vats

Try setting property IsSynchronizedWithCurrentItemto truefor your combobox and remove the trigger completely -

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

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

回答by eran otzap

has in addition to the current answers which led me on the way to my solution what i actually needed to accomplish is retrieving the last selected item when switching between itemsSources (plural)

除了当前的答案,这些答案让我走上了我的解决方案,我真正需要完成的是在 itemsSources(复数)之间切换时检索最后一个选定的项目

from an article i found : "for each ItemsSource binding, a unique CollectionView is generated.."

从我发现的一篇文章中: “对于每个 ItemsSource 绑定,都会生成一个唯一的 CollectionView..”

i concurred that as long as the view exists each binding would generate its own CollectionView and thus hold a reference to CurrentItem and CurrentPosition if decorated with

我同意,只要视图存在,每个绑定都会生成自己的 CollectionView,因此如果装饰有对 CurrentItem 和 CurrentPosition 的引用

IsSynchronizedWithCurrentItem="True"

IsSynchronizedWithCurrentItem="True"

so i created my own ChangePropertyActionClass :

所以我创建了自己的ChangePropertyAction类:

 public class RetriveLastSelectedIndexChangePropertyAction : ChangePropertyAction
 {                
    public int LastSelectedIndex
    {
        get { return (int)GetValue(LastSelectedIndexProperty); }
        set { SetValue(LastSelectedIndexProperty, value); }
    }

    public static readonly DependencyProperty LastSelectedIndexProperty =
        DependencyProperty.Register("LastSelectedIndex", typeof(int), typeof(RetriveLastSelectedIndexChangePropertyAction), new UIPropertyMetadata(-1));

    protected override void Invoke(object parameter)
    {
        var comboBox = this.AssociatedObject as ComboBox;
        this.SetValue(LastSelectedIndexProperty, comboBox.Items.CurrentPosition);            
    }        
 }

and invoked it using PropertyChangedTriggeras follows :

并使用PropertyChangedTrigger调用它 ,如下所示:

 <ComboBox ItemsSource="{Binding SelectedItemsSource, Mode=OneWay}" 
           x:Name="c1" 
           IsSynchronizedWithCurrentItem="True">                                    
       <i:Interaction.Triggers>
          <ei:PropertyChangedTrigger Binding="{Binding ElementName=c1,Path=ItemsSource}">
              <local:RetriveLastSelectedIndexChangePropertyAction                   
                      PropertyName="SelectedIndex" 
                      Value="{Binding LastSelectedIndex}" 
                      TargetName="c1"/>
          </ei:PropertyChangedTrigger>                        
       </i:Interaction.Triggers>
  </ComboBox>            

hope this helps if any one needing to retrieve their last selected item with out any messy code in their DataContext , enjoy.

如果有人需要在 DataContext 中没有任何凌乱的代码的情况下检索他们最后选择的项目,希望这会有所帮助。

回答by K Mehta

Try changing your binding from:

尝试更改您的绑定:

<ei:PropertyChangedTrigger Binding="{Binding ItemsSource,RelativeSource={RelativeSource Self}}">

to

<ei:PropertyChangedTrigger Binding="{Binding ItemsSource,ElementName=c1}">