1

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>
Matt
  • 7,004
  • 11
  • 71
  • 117
  • Have you tried binding to SelectedValue? I think binding to SelectedItem will give you a Framework element – Martin Grundy Feb 19 '19 at 09:20
  • @MartinGrundy, no luck. I suspect the button commandparameter cannot pick up the elementname of an element that is defined subsequently? Or I have to use a RelativeSource within the command? Stuck... – Matt Feb 19 '19 at 09:23
  • Using Relative source on the CommandParameter biding is worth a shot. Although as you are using the element name directly is usually fine. Do you get an error in the output window when debugging regarding the binding resolution error? Thats usually where I find the answer to any issues I'm having – Martin Grundy Feb 19 '19 at 09:28
  • I get "Cannot find source for binding with reference 'ElementName=AssetClassInButton'. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'SimpleButton' (Name=''); target property is 'CommandParameter' (type 'Object')". I am afraid I do not know how to point the binding to a child that sits within the button's own content template. – Matt Feb 19 '19 at 09:32

1 Answers1

3

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>
Martin Grundy
  • 254
  • 2
  • 11
  • Perfect, that worked right out of the box. Just wondering why it could not find the element inside the template. Is there an issue accessing elements that are children inside an element's template? – Matt Feb 19 '19 at 09:48
  • 1
    When using content templates you are usually either switching between different templates or using them for reocurring elements (such as an ItemsControl). These cannot easily be accessed by parent's in the visual tree as they are not guarenteed to be there (e.g. if you switch out the template, or there could be multiple instances for a ItemsControl). From a template it is ealier to walk up the visual tree using RelativeSource if required, I haven't yet had a need to do this. - Hope this helps? – Martin Grundy Feb 19 '19 at 10:02
  • Thanks for the explanation. I do not need a template for this particular problem hence you solved my problem. Thanks. Should I need a switchable template via templateselector in the future then I will raise a new question. – Matt Feb 19 '19 at 10:11