0

My WPF uses the MVVM approach. I'm trying to bind 2 controls within my list control

<ListBox ItemsSource="{Binding ParentDuplicate}" SelectedItem="{Binding SelectedParent, UpdateSourceTrigger=PropertyChanged}">
    <ListBox.ItemTemplate>
         <DataTemplate>
             <StackPanel>
                    <ContentControl Content="{Binding}" />
                    <Button Content="Delete me now" 
                            Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, Path=DeleteCommand}" 
                            CommandParameter="{Binding FilePath}" />
              </StackPanel>
           </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The problem I'm having, is the DeleteCommand is not binding (the Ouput window informs me as well)

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Data.Binding', AncestorLevel='1''. BindingExpression:Path=DeleteCommand; DataItem=null; target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')

If I move this button to outside the ListBox, then the binding works and the event fires so I know the issue must be with the ListBox (I'm guessing the problem is the ItemsSource prevents the binding to anything but the ItemsSource bound property (in this case, ParentDuplicate))

So, within the Button control, there are 2 properties being bound, DeleteCommand and FilePath

Both of these properties live within my single ViewModel. FilePath is a child of ParentDuplicate and this binds as desired. The issue is only with the DeleteCommand. What am I doing wrong?

Edit

When I use the Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, Path=DeleteCommand} it is looking in my MainWindow code behind, not at the ViewModel. How do I make it use the ViewModel?

I tried

Command="{Binding RelativeSource={RelativeSource  AncestorType=xmlnsViewModel:MainWindowViewModel}, Path=DeleteCommand}" 

but the above also results in the same binding errors

1
  • 1
    Basically RelativeSource searches the Visual Tree and your DataContext/ViewModel is not in there. It is rather a property to a visual element. Commented Dec 11, 2013 at 16:19

1 Answer 1

3

The ancestor search finds the control, not the DataContext, so you'll need to tell your binding where to find the DeleteCommand property. If your ViewModel is the DataContext of the MainWindow then you can just use:

<Button Content="Delete me now" 
    Command="{Binding RelativeSource={RelativeSource 
                  Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, 
                  Path=DataContext.DeleteCommand}" 
    CommandParameter="{Binding FilePath}" />
Sign up to request clarification or add additional context in comments.

1 Comment

I ended up using Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType=Grid}, Path=DataContext.DeleteCommand}" Thank you

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.