1

I got following listbox

<Page.DataContext>
    <self:NewUserViewModel/>
</Page.DataContext>
<ListBox x:Name="PermissionLbox"  ItemsSource="{Binding ListFromDataContext}" Height="75" Margin="10,117,10,0" VerticalAlignment="Top">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding .}"></CheckBox>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Why if I change CheckBox definition to:

<CheckBox Command={Binding CommandFromDataContext} Content="{Binding .}"></CheckBox><CheckBox   Content="{Binding .}"></CheckBox>

name CommandFromDataContex cannot be resolved although CommandFromDataContext exists in NewUserViewModel. The ListFromDataContext is a property of NewUserViewModel, Command either, but Command is not resolved.

igorr
  • 141
  • 8

1 Answers1

4

The datacontext of the checkbox isnt the viewmodel any more, but rather the individual item it instances in the listbox.

You need to bind to a RelativeSource and Path to get to the ViewModel again.

See existing answer for details.

Community
  • 1
  • 1
Mattias Åslund
  • 3,877
  • 2
  • 18
  • 17