Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 正确使用PropertyChangedTrigger和ChangePropertyAction_Wpf_Xaml - Fatal编程技术网

Wpf 正确使用PropertyChangedTrigger和ChangePropertyAction

Wpf 正确使用PropertyChangedTrigger和ChangePropertyAction,wpf,xaml,Wpf,Xaml,当我的组合框上的ItemsSource属性发生更改时,我正在尝试设置默认的选定值 我的xaml: <ComboBox ItemsSource="{Binding SelectedItemsSource, Mode=OneWay}" x:Name="c1"> <i:Interaction.Triggers> <ei:PropertyChangedTrigger Binding="{Binding ItemsSource,RelativeSour

当我的
组合框上的
ItemsSource
属性发生更改时,我正在尝试设置默认的选定值

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

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


你知道为什么这样不行吗

尝试从以下位置更改绑定:

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


尝试将组合框的属性设置为
true
,并完全移除触发器-

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

除了当前的答案之外,还有其他的答案,这些答案引导我找到了解决方案 我实际需要完成的是在 在项目资源之间切换(复数)

我在一篇文章中发现:

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

IsSynchronizedWithCurrentItem=“True”

因此,我创建了自己的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);            
    }        
 }
并使用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>            

希望这有助于任何人需要检索他们最后选择的项目没有任何混乱
在他们的DataContext中编写代码,尽情享受吧

当你给你的组合框起一个名字,并在你的绑定中使用ElementName而不是RelativeSource时,会发生什么呢。。。谢谢我猜可能交互触发器不应该像在视觉树中与元素相关一样引用元素。这是我的猜测。我将把这条评论作为答案转发给大家,这样我们就可以结束这个问题:)实际上什么都没做。。我会再试一次,让你知道
 <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>