I'm trying to develop a simple register in C# with WPF. I have buttons for my products and if they are pressed (via Keyboard shortcut or mouse button) the product is ordered.
I don't know how much products I have (will be loaded from the database) so a fixed solution is not what I want.
I've managed to display those buttons via a Listbox
bound to the objects.
<ListView.ItemTemplate>
<DataTemplate>
<UniformGrid>
<Button Template="{DynamicResource ButtonBaseControlTemplate1}" Style="{StaticResource ButtonStyle1}" Command="{Binding OrderCommand}" CommandParameter="{Binding}">
<Button.Background>
<ImageBrush ImageSource="{Binding PictureUrl}" />
</Button.Background>
<DockPanel>
<TextBlock Text="{Binding Name}" FontSize="30" DockPanel.Dock="Top" HorizontalAlignment="Center" Margin="0, 25, 0, 0"/>
<TextBlock Text="{Binding Price}" FontSize="15" HorizontalAlignment="Left" Margin="5"/>
<TextBlock Text="{Binding Shortcut}" FontSize="15" HorizontalAlignment="Right" DockPanel.Dock="Bottom" VerticalAlignment="Bottom" Margin="5"/>
</DockPanel>
</Button>
</UniformGrid>
</DataTemplate>
</ListView.ItemTemplate>
Binding a Command
does not work. If I click on the button, nothing happens. If I use the Clicked
event, everything works fine, but I need the object which is bound to the button as parameter.
Here's my command property:
public RelayCommand OrderCommand
{
get
{
return new RelayCommand((p) => MessageBox.Show("Test"), (p) => true);
}
}
If everything would work as intended, there should be a MessageBox
displaying "Test".
Thanks for your help in advance.
Regards, Stefan
You will have to update your command binding to find it in your Window/Usercontrol datacontext
Assuming Command is in Windows DataContext
You have to change the datacontext of your button. As it is now, the datacontext is the object in the listview. What you want is to use the datacontext of your listview.
Something like that