0

I have been trying to bind a few list items to a property in a parent ViewModel.

XAML code:

<UserControl x:Class="MicroFilmSearch.Views.SearchPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:MicroFilmSearch.Views"
  xmlns:vm="clr-namespace:MicroFilmSearch.ViewModels"
  xmlns:con="clr-namespace:MicroFilmSearch.Converters"
  mc:Ignorable="d"
  Width="1557.5" Height="Auto">
<UserControl.Resources>
<vm:SearchPageViewModel x:Key="VM"/>
  </UserControl.Resources>
<UserControl.DataContext>
    <vm:SearchPageViewModel />
</UserControl.DataContext>


...SNIP...


<ListView ItemsSource="{Binding DetailsFilm.Person}" 
        Background="{x:Null}" 
        BorderBrush="{x:Null}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <StackPanel Visibility="{Binding AreDetailsVisible, Source={StaticResource VM}}">
                    <TextBlock x:Name="PersonDetails" 
                        Text="{Binding PersonName}"/>
                </StackPanel>
                <StackPanel Visibility="{Binding AreEditorsVisible, Source={StaticResource VM}}">
                    <TextBox Text="{Binding PersonNameName, Mode=TwoWay}"/>
                    <CheckBox Content="Delete" 
                            IsChecked="{Binding IsMarkedForDeletion, Mode=TwoWay}"/>
                </StackPanel>
                </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>

There isn't a problem here from the standpoint of functionality. I am able to access the property from the parent VM. What I am concerned about, is if I do this:

<UserControl.Resources>
   <vm:SearchPageViewModel x:Key="VM"/>
</UserControl.Resources>

And then this:

<UserControl.DataContext>
   <vm:SearchPageViewModel />
</UserControl.DataContext>

Am I actually making a SearchPageViewModel instance twice?

If that is the case, what would be the correct way to go about binding to a parent ViewModel from a ListItem?

Thank you, all.

Gary.Taylor717
  • 163
  • 1
  • 13

0 Answers0