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.