2

I'm working on a MVVM-project using XAML. I'm having problems accessing a property of an outer element when operating inside a ListView with an ItemSource binding.

XAML:

<ListView x:Name="BibTexFields" Height="422" ItemsSource="{Binding BibTexFields}">
    <ListView.ItemTemplate>
        <DataTemplate>
           <Grid Width="450">
              <TextBlock Foreground="Black" Text="{Binding Field.Name}"/>
              <CheckBox HorizontalAlignment="Right" IsChecked="{Binding IsChecked, Mode=TwoWay}" Command="{Binding UpdateFilledFieldsCommand}"/> 

BibTexFields is a property from my ViewModel which also is my DataContext. So is UpdateFilledFieldsCommand

XAML:

xmlns:vm="using:StudyConfigurationClient.ViewModels"
mc:Ignorable="d" d:DataContext="{d:DesignInstance vm:CreateStudyViewModel}">

<Grid x:Name="FieldOuter">
    <Border BorderBrush="Gray" BorderThickness="2" Margin="0, 0, 5, 0">

ViewModel:

private ObservableCollection<ChosenField> _bibTexFields;
public ObservableCollection<ChosenField> BibTexFields
        {
            get { return _bibTexFields; }
            set
            {
                _bibTexFields = value;
                OnPropertyChanged();
            }
        }

What I want to do is access the ViewModel from inside the ListView, so that I can make a Command with a binding to the UpdateFilledFieldsCommand property.

I've tried researching the RelativeSource binding, but can't seem to make it work. Nor does trying binding it to the DataContext work.

Romasz
  • 29,662
  • 13
  • 79
  • 154

1 Answers1

1

You can also use ElementName:

 <CheckBox HorizontalAlignment="Right" IsChecked="{Binding IsChecked, Mode=TwoWay}" Command="{Binding DataContext.UpdateFilledFieldsCommand, ElementName=BibTexFields}"/>

However, RelativeSource should also work. With a quick search, I found this: How do I use WPF bindings with RelativeSource? It looks fine though I don't know if all the features are present in an UWP app.

Community
  • 1
  • 1
Frosty
  • 46
  • 4