0

I have a custom TabControl with an ItemsSource binded to it. Inside the ItemTemplate is a Button which Command I want to bind to a RelayCommand from my DataContext (ViewModel).

The Problem is, that the application is looking for the Command inside my ItemsSource Item.

<TabControl ItemsSource="{Binding ViewModel.TabItems,UpdateSourceTrigger=PropertyChanged}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <!-- The "Header" is from the "TabItems" Model -->
                <TextBlock Text="{Binding Header}" />
                <!-- I want to bind this Command outside the "TabItems" Model -->
                <Button Command="{Binding ViewModel.CounterIncrementCommand, Mode=OneWay}"/>
            </DockPanel>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <TextBox>Test</TextBox>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

I get the Binding Failures:

  • DataContext: TabControllitemModel
  • Binding Path: ViewModel
  • Target: Button.Command
  • Target Type: ICommand
  • Description: ViewModel property not found on object of type TabControlItemModel

1 Answer 1

0

Okay, I found it out. With this I can set the DataContext of the Button to the same one of the Window/Page:

<Button DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}}" Command="{Binding ViewModel.CounterIncrementCommand}"/>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.