Goal:
Need to make selection of a single row included button in the listview.
Problem:
Don’t know how to make a single selection in the listview when you are clicking on the button that is located inside of listview. In a simple explanation, when clicking on the button, no selection will be applied in the listview’s row
It would be great if selection is applied before entering button’s method cuz i need to grab information from the single selection of the listview that should be sent to business logic.
private void btnBuy_Click(object sender, RoutedEventArgs e)
{
}
<ListView Height="242.47" Canvas.Left="8" Canvas.Top="49.53" Width="435.22" Name="lstOrder" ItemsSource="{Binding}"
PreviewMouseLeftButtonUp="lstOrder_PreviewMouseLeftButtonUp" SelectionChanged="lstOrder_SelectionChanged"
SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Article Number" Width="auto" DisplayMemberBinding="{Binding Path=_articleNumber}"
TextBlock.TextAlignment="Left"
HeaderContainerStyle="{StaticResource ListViewHeaderRightAlignedStyle}" />
<GridViewColumn Header="Name" Width="auto" DisplayMemberBinding="{Binding Path=_name}"
TextBlock.TextAlignment="Left"
HeaderContainerStyle="{StaticResource ListViewHeaderRightAlignedStyle}" />
<GridViewColumn Header="Sale Price" Width="auto" DisplayMemberBinding="{Binding Path=_salePrice}"
TextBlock.TextAlignment="Left"
HeaderContainerStyle="{StaticResource ListViewHeaderRightAlignedStyle}" />
<GridViewColumn Header="Product Category" Width="auto"
DisplayMemberBinding="{Binding Path=_productCategory}" TextBlock.TextAlignment="Left"
HeaderContainerStyle="{StaticResource ListViewHeaderRightAlignedStyle}" />
<GridViewColumn Header="Quantity" Width="auto" DisplayMemberBinding="{Binding Path=_quantity}"
TextBlock.TextAlignment="Left"
HeaderContainerStyle="{StaticResource ListViewHeaderRightAlignedStyle}" />
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Name="btnBuy" MinHeight="20" MinWidth="50" Content="Buy" Click="btnBuy_Click" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
namespace MediaStore
{
/// <summary>
/// Interaction logic for Counter.xaml
/// </summary>
public partial class Counter : Window
{
private ManagerProduct _myManagerProduct;
private ManagerCart _myManagerCart;
public Counter(ManagerProduct pManagerProduct, ManagerCart pMyManagerCart)
{
this.InitializeComponent();
_myManagerProduct = pManagerProduct;
_myManagerCart = pMyManagerCart;
lstOrder.AddHandler(ListViewItem.UnselectedEvent, new RoutedEventHandler(ItemSelected), true);
UpDateGUI();
}
public void UpDateGUI()
{
FillDataInListView();
}
private void FillDataInListView()
{
lstOrder.DataContext = _myManagerProduct.GetAllProductList();
}
#region Tab Order - 1. Order
private void btnBuy_Click(object sender, RoutedEventArgs e)
{
}
private void btnDisplayCart_Click(object sender, RoutedEventArgs e)
{
}
private void btnCheckout_Click(object sender, RoutedEventArgs e)
{
cvsOrder.Visibility = Visibility.Hidden;
cvsConfirmation.Margin = new Thickness(8, 8, 8, 17);
}
private void lstOrder_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
}
private void ItemSelected(object sender, RoutedEventArgs e)
{
}
private void lstOrder_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
#endregion
}
}
You should not need to select the ListViewItem, you can get your dataobject from the
DataContextof theButtonand you don’t need the data from other rows since you wanted a single selection anyway.In the event-handler:
(On a side note, that being said, do you need selection at all? If not use an
ItemsControl)To make the
ListViewItemselect itself if the button is clicked you can use aItemContainerStylelike this:Here is an alternative using an animation which preserves the selected item after the
ListViewlooses focus (only works withListView.SelectionModebeingSingleas it does not seem to be possible to clear the previous selection using an animation):