繁体   English   中英

WPF ComboBox 不会在初始化时触发事件

[英]WPF ComboBox will not trigger an event on initializing

我有以下情况: 1. 我有 Window Load 事件 - 它获取 ComboBox 的 SourceItems 2. ComboBox 有 EventTrigger for Selection Changed 和以下 XAML:

    <ComboBox x:Name="uxEnvironmentsComboBox" 
         ItemsSource="{Binding Environments}" Width="90" Margin="0,10,160,0" 
         HorizontalAlignment="Right"  
         VerticalContentAlignment="Center" 
         HorizontalContentAlignment="Center"
         IsSynchronizedWithCurrentItem="True" SelectedIndex="0">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <i:InvokeCommandAction Command="{Binding SelectEnvironment}"
                                                   CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>

一切正常,窗口加载正常,列表填充在 ComboBox 中,但是当进行默认选择时,我的<i:EventTrigger EventName="SelectionChanged">不会触发并且不会触发其余的应用程序配置,除非我做手动选择更改?!

SelectEnvironment应该是您绑定到的属性:

<ComboBox x:Name="uxEnvironmentsComboBox" 
         ItemsSource="{Binding Environments}" Width="90" Margin="0,10,160,0" 
         SelectedItem="{Binding SelectEnvironment}"
         HorizontalAlignment="Right"  
         VerticalContentAlignment="Center" 
         HorizontalContentAlignment="Center"
         IsSynchronizedWithCurrentItem="True" SelectedIndex="0">
</ComboBox>

如果您想,您可以在此属性的设置器中调用任何命令:

private Environment _selectEnvironment;
public Environment SelectEnvironment
{
    get { return _selectEnvironment; }
    set
    {
        _selectEnvironment = value;
        //invoke command
        YourCommand.Execute(_selectedEnvironment);
    }
}

如果你想调用命令或初始设置属性,你可以例如在视图模型的构造函数中执行此操作。 您不需要使用InvokeCommandAction

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM