ComboBox inside Button, how to pass "SelectedItem" of Combobox as CommandParameter of Button?

678 Views Asked by At

I am not able to access SelectedItem of Combobox that resides inside a button. I want to pass the SelectedItem as CommandParameter of the Button to my VM. Inside my VM I use MVVMLight's ICommand<T>.

What am I doing wrong?

<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

There are 1 best solutions below

3
Martin Grundy On BEST ANSWER

Get rid of the ContentTemplate\DataTemplate - you dont need it as you are setting the content of the Button directly and not the template of a reoccurring element.

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