Button内的ComboBox,如何将Combobox的“ SelectedItem”作为Button的CommandParameter传递?

时间:2019-02-19 09:12:52

标签: c# wpf devexpress commandbinding

我无法访问位于按钮内的Combobox的SelectedItem。我想将SelectedItem作为Button的CommandParameter传递给我的VM。在我的VM内,我使用MVVMLight的ICommand<T>

我在做什么错了?

<dx:SimpleButton Margin="0,5,0,5" MinWidth="160" Command="{Binding CreateNewSymbolCommand}" CommandParameter="{Binding ElementName=AssetClassInButton, Path=SelectedItem}">
            <dx:SimpleButton.ContentTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBox Text="Choose Asset Class" Foreground="LightGreen" HorizontalAlignment="Center"/>
                        <dxe:ComboBoxEdit Name="AssetClassInButton" MinWidth="150" IsTextEditable="False" ItemsSource="{Binding Source={StaticResource AssetClassEnumValues}}"/>
                    </StackPanel>

                </DataTemplate>
            </dx:SimpleButton.ContentTemplate>
        </dx:SimpleButton>

1 个答案:

答案 0 :(得分:3)

摆脱ContentTemplate \ DataTemplate-在直接设置Button的内容而不是重复出现的元素的模板时,不需要它。

<dx:SimpleButton Margin="0,5,0,5" MinWidth="160" Command="{Binding CreateNewSymbolCommand}" CommandParameter="{Binding ElementName=AssetClassInButton, Path=SelectedItem}">
    <StackPanel Orientation="Vertical">
        <TextBox Text="Choose Asset Class" Foreground="LightGreen" HorizontalAlignment="Center"/>
        <dxe:ComboBoxEdit Name="AssetClassInButton" MinWidth="150" IsTextEditable="False" ItemsSource="{Binding Source={StaticResource AssetClassEnumValues}}"/>
    </StackPanel>
</dx:SimpleButton>