Programmatically Adding Buttons in MVVM but Command doesn't work

611 Views Asked by At

I'm trying to add buttons dynamically and it's work but command doesn't work.

I'm getting error

ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='RadRibbonButton'

<telerik:RadRibbonGroup>
 ...
<telerik:RadRibbonGroup.DataContext>
    <vm:Group1/>
</telerik:RadRibbonGroup.DataContext>

        <ItemsControl ItemsSource="{Binding ButtonsCollection}"  >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding}" Command="{Binding DataContext.ButtonCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadRibbonGroup}}}"   />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

</telerik:RadRibbonGroup>



ButtonCommand= new DelegateCommand(ExecuteCommand);

private void ExecuteCommand()
{
     ...
}
2

There are 2 best solutions below

0
On
  1. It doesn't reach
  2. ObservableCollection of buttons(and I can see the buttons in the view)

3.I should create and add buttons to collection runtime(There are special logic behind this).In constructor I create command

0
On
  1. Check that it does reach the command in debug mode.
  2. Is your ButtonsCollection is dependency property? do you raise its change?
  3. You are not supposed to add actual buttons to your buttons collection, thats is not pure mvvm. you need to create the buttons in a data template, and in the collection you need to create the properties like the button's content etc.
  4. If you still insist creating actual buttons, i think that templating "" won't work. Try "