Home > Software engineering >  Make an event bubble up from the children upwards
Make an event bubble up from the children upwards

Time:03-16

AFAIK bubbling means that if an event is triggered on the children, the same event will be triggered on the parents.

I have this piece of code:

<ListView Name="lvFiles" SelectedItem="{Binding SelectedTabFilePath, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
                      ItemsSource="{Binding Path=SelectedItem.Files,ElementName=trvFiles, Mode=OneWay}"
                      >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Path=Name}"
                                Command="{Binding DataContext.OpenFileFromTreeView,
                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
                                CommandParameter="{Binding DataContext,
                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
                                Background="Transparent"
                                BorderBrush="Transparent"
                                >
                        </Button>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

The problem is that if I click on the button, the property SelectedTabFilePath from SelectedItem will not be filled with data (because I didn't click on the item of the ListView).

So, my question is: is there any way to trigger the click event of the button and that of the ListView too?

Or if not, what would be the way to set the SelectedTabFilePath from the SelectedItem attribute when I click on that button, too?

Thanks.

CodePudding user response:

how to handle WPF listbox selectionchanged event using MVVM

I think you could use interaction to trigger the same command(OpenFileFromTreeView) when the listbox selection changes.

CodePudding user response:

Instead of:

AncestorType={x:Type ListView}

What if you try:

AncestorType={x:Type ListViewItem}
  • Related