Z
Z
ZelibobA17062017-02-27 10:14:58
WPF
ZelibobA1706, 2017-02-27 10:14:58

How to set different datacontext properties of one object?

There is a viewmodel MainWindowVM. The view has an itemscontrol bound to the collection.

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Button Command="{Binding }" CommandParameter="{Binding Value}" Content="{Binding Value}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

How do I specify a command from MainWindowVM if the datecontext is an ItemsSource and it only points to the items in the collection?
I tried to write like this:
<Button Command="{Binding ButtonClickCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type viewModels:MainWindowVM}}}" CommandParameter="{Binding Value}" Content="{Binding Value}" />

But that doesn't work either.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
ZelibobA1706, 2017-02-27
@ZelibobA1706

Solved in this way:

<Button Margin="3" Command="{Binding DataContext.ButtonClickCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" CommandParameter="{Binding Value}" Content="{Binding Value}" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question