列表视图中的单个选择按钮

时间:2011-06-06 16:41:50

标签: c# wpf xaml listview selection

目标:
需要在列表视图中选择包含单行的按钮。

问题:
当您点击listview内部的按钮时,不知道如何在列表视图中进行单个选择。在一个简单的解释中,当点击按钮时,列表视图的行中不会应用任何选择

如果在进入按钮的方法之前应用选择会很棒,因为我需要从列表视图的单个选择中获取应该发送到业务逻辑的信息。


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

    }

}

1 个答案:

答案 0 :(得分:4)

您不需要选择ListViewItem,您可以从DataContext的{​​{1}}获取数据对象,并且您不需要其他行中的数据,因为您仍然需要单个选择。

在事件处理程序中:

Button

在旁注上,据说,您是否需要选择?如果不使用var data = (sender as FrameworkElement).DataContext as MyData;

要点击按钮,ItemsControl选择自己,您可以使用ListViewItem,如下所示:

ItemContainerStyle

以下是使用动画的替代方法,该动画会在<ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="IsSelected" Value="{Binding RelativeSource={RelativeSource Self}, Path=IsKeyboardFocusWithin, Mode=OneWay}" /> </Style> </ListView.ItemContainerStyle> 失去焦点后保留所选项目(仅适用于ListView ListView.SelectionMode,因为似乎无法清除以前的选择使用动画):

Single