0

I currently have a ListBox that is bound to a collection of objects and displays a button for each object in the collection.

XAML:

<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>

    <ListBox x:Name="company_buttons"
             HorizontalContentAlignment="Left" VerticalContentAlignment="Top" 
             Padding="-2,-2,0,0" Height="280" Width="300" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0,0" BorderThickness="0" 
             ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ItemsSource="{Binding Buttons_Binding}">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>

                    <Button Background="#FFFCC515" Style="{StaticResource RoundCorner}" FontFamily="Segoe UI" FontSize="16" FontWeight="Bold" TabIndex="0" 
                                CommandParameter="{Binding This_Works}"
                                Command="{Binding HELP WITH THIS}">
                        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Width="270" Height="44">
                            <Label HorizontalContentAlignment="Center" VerticalContentAlignment="Top" Margin="5,0,5,0" Height="21" Padding="0" Content="{Binding This_Works}"/>
                            <Label HorizontalContentAlignment="Center" VerticalContentAlignment="Top" Margin="5,0,5,0" Height="21" Padding="0" Content="{Binding This_Works}"/>
                        </StackPanel>
                    </Button>

                    <Label VerticalContentAlignment="Top" Margin="5,0,5,0" Height="19" Padding="0" Foreground="White" FontFamily="Segoe UI" FontSize="12" FontWeight="Bold" Content="{Binding Path}"/>

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

    </ListBox>

Here are the things that are used in the bindings:

public class ButtonContent : INotifyPropertyChanged
{
 // stuff
}

public class MainWindowViewModel
{
    public static ObservableCollection<ButtonContent> Buttons_Binding
    {
 // stuff
    }

    public ICommand Command
    {
 // stuff
    }

My problem is I do not know how to bind the button click to Command since it is outside of the collection Buttons_Binding and hence not in the same data context. All the stuff I have seen from examples and what I have tried have all failed.

David Bentley
  • 824
  • 1
  • 8
  • 27

1 Answers1

1

Since each Button is bound to the ListBoxItem's DataContext, it will not have access to the ListBox's DataContext. However, you can traverse the visual tree using RelativeSource in the binding. Try this

<Button ... 
        Command="{Binding Path=DataContext.Command,
                  RelativeSource={RelativeSource AncestorType=ListBox}}">
    ...
</Button>
Clemens
  • 123,504
  • 12
  • 155
  • 268
Ayyappan Subramanian
  • 5,348
  • 1
  • 22
  • 44
  • That worked. For some reason I could find the answer in a search. I also found a really clumsy way of making it work. I made the function static and then did this: `Command="{Binding Source={x:Static local:MainWindowViewModel.Command}}"` – David Bentley Nov 15 '17 at 21:08