1
votes

How to bind the Command of the Button "myCmdButton" to the ParameterVariableList, like the "addBtn" do. The "addBtn" works like expected. I want the other button command to my ParameterVariableList class too.

Here's the XAML:

<DataTemplate DataType="{x:Type locale:ParameterVariableList}" >
    <StackPanel Orientation="Vertical">

        <TextBlock Text="{Binding Description}"/>
        <Button Content="add" Command="{Binding AddEntryCmd}" Name="addBtn" />

        <ListBox ItemsSource="{Binding ItemList, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch" >
            <ListBox.ItemTemplate>
                <ItemContainerTemplate>
                    <StackPanel Orientation="Horizontal">

                        <TextBlock Text="{Binding}" />

                        <Button Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" Command="{Binding RemoveEntryCmd, ???}" Name="myCmdButton" >
                            <Image Source="Resources/trash_16x16.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" />
                        </Button>

                    </StackPanel>
                </ItemContainerTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</DataTemplate>

I expected this to work, but it doesn't:

Command="{Binding RemoveEntryCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type locale:ParameterVariableList}}}"

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

2

2 Answers

1
votes

This works:

Command="{Binding DataContext.RemoveEntryCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"

Nearly the answer of Abhinav, but DataContext was missing.

0
votes

This should work

Command="{Binding RemoveEntryCmd,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBox}}}