繁体   English   中英

WPF ComboBox Mvvm绑定

[英]WPF ComboBox Mvvm binding

我试图从列表中获取选定的值到命令中。 有没有简单的方法可以将commandparameter绑定到所选项目?

<ComboBox HorizontalAlignment="Left" Margin="-56,10,0,0" VerticalAlignment="Top" Width="120" SelectedIndex="0" ItemsSource="{Binding Time}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding }"></i:InvokeCommandAction>
            </i:EventTrigger>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding }"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ComboBox>

当我尝试像

                <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}},Path=SelectionBoxItem}"></i:InvokeCommandAction>
            </i:EventTrigger>

奇怪为什么卡住了?

<ComboBox x:Name="Combo" HorizontalAlignment="Left" Margin="-56,10,0,0" VerticalAlignment="Top" Width="120" SelectedIndex="0" ItemsSource="{Binding Time}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding ElementName=Combo, Path=SelectedItem}"></i:InvokeCommandAction>
            </i:EventTrigger>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding }"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ComboBox>

如果给组合框命名,则可以使用ElementName绑定并绑定到Path SelectedItem

暂无
暂无

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

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