WPF HandleListBoxClickEvent仅适用于列表的空白区域

时间:2015-07-08 08:50:13

标签: c# wpf xaml listbox mouseclick-event

为什么HandleListBoxClickEvent仅适用于列表的空白区域?问题是当我想刷新列表时。点击某个项目时 - 没有任何更新。但是单击列表的空白区域(选择某个项目时) - 所选项目正在更新。

我认为,某些问题应该在xaml中,因为我正在使用c#代码跟随一个准备好的(工作)示例。

资源:

    <UserControl.Resources>
    <Style x:Key="RedGlowItemContainer" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border Name="IconBorder" Background="#00FFFFFF">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ListBoxItem.IsSelected" Value="True">
                            <Setter TargetName="IconBorder" Property="Border.Background" Value="#FF07A3E9" />
                            <Setter Property="Control.FontWeight" Value="Bold" />
                            <Setter Property="Control.Foreground" Value="#FFFFFFFF" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <DataTemplate x:Key="CategoryTemplate" DataType="{x:Type vm:OrderEntryViewModel}">
        <vw:MenuItemSelectorView />
    </DataTemplate>
    <mmc:OrderTemplateSelector x:Key="PanelTemplateSelector" />
</UserControl.Resources>

视图模型:

internal class OrderEntryViewModel : ViewModelBase, IParentViewModel, IViewModelBase
{
...
public void HandleListBoxClickEvent()
    {
        if (this._selectedOrder != null)
        {
            if (this._selectedOrder.IsNew)
            {
                if (this._qty != "")
                {
                    this._selectedOrder.Quantity = int.Parse(this._qty);
                    this.SelectedQuantity = "";
                    this.CalculateTotal();
                }
            }
        }
}
...
}

xaml.cs:

public partial class OrderEntryView : System.Windows.Controls.UserControl
{
    private bool isExpended = true;

    public OrderEntryView()
    {
        this.InitializeComponent();
    }

    private void LstTicket_MouseDown(object sender, MouseButtonEventArgs e)
    {
        ((OrderEntryViewModel)base.DataContext).HandleListBoxClickEvent();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
    }
}

xaml list:

 <ListBox Name="LstTicket" IsSynchronizedWithCurrentItem="True" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" ItemContainerStyle="{StaticResource RedGlowItemContainer}" FontSize="12" ItemsSource="{Binding Orders, Mode=TwoWay}" SelectedItem="{Binding SelectedOrder, Mode=TwoWay}" Common:ListBoxExtenders.AutoScrollToEnd="True" MouseDown="LstTicket_MouseDown" Grid.Row="1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel HorizontalAlignment="Stretch" Background="#00FFFFFF">
                        <Grid Background="#00FFFFFF" Width="230">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="24" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="45" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="25" />
                            </Grid.RowDefinitions>
                            <TextBlock Style="{DynamicResource StyleStrikethrough}" Grid.Column="0" Width="24" Padding="0" HorizontalAlignment="Left" Text="{Binding Quantity, StringFormat=0}" />
                            <TextBlock Style="{DynamicResource StyleStrikethrough}" Grid.Column="1" HorizontalAlignment="Left" Padding="0" Text="{Binding DisplayName}" />
                            <TextBlock Style="{DynamicResource StyleStrikethrough}" Grid.Column="2" Padding="0" HorizontalAlignment="Right" Text="{Binding TotalPrice, StringFormat=N2}" />
                        </Grid>
                        <ItemsControl ItemsSource="{Binding OrderModifiers}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid Background="#00FFFFFF">
                                        <Label Padding="0" Foreground="#FF0000FF" Margin="30,0,0,0" HorizontalAlignment="Left" Content="{Binding DisplayName}" />
                                        <Label Padding="0" Foreground="#FF0000FF" Margin="0,0,20,0" HorizontalAlignment="Right" Content="{Binding Price, StringFormat=N2}" />
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <ItemsControl ItemsSource="{Binding OrderSetItems}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <Grid Background="#00FFFFFF" Width="230">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="24" />
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="45" />
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="15" />
                                            </Grid.RowDefinitions>
                                            <TextBlock Style="{DynamicResource StyleStrikethrough}" Margin="10,0,0,0" Grid.Column="0" Width="24" Padding="0" HorizontalAlignment="Left" Text="{Binding Quantity, StringFormat=0}" />
                                            <TextBlock Style="{DynamicResource StyleStrikethrough}" Grid.Column="1" HorizontalAlignment="Left" Padding="0" Text="{Binding DisplayName}" />
                                            <TextBlock Style="{DynamicResource StyleStrikethrough}" Grid.Column="2" Padding="0" HorizontalAlignment="Right" Text="{Binding TotalPrice, StringFormat=N2}" />
                                        </Grid>
                                        <ItemsControl ItemsSource="{Binding OrderSetModifiers}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid Background="#00FFFFFF">
                                                        <Label Padding="0" Foreground="#FF0000FF" Margin="30,0,0,0" HorizontalAlignment="Left" Content="{Binding DisplayName}" />
                                                        <Label Padding="0" Foreground="#FF0000FF" Margin="0,0,20,0" HorizontalAlignment="Right" Content="{Binding Price}" />
                                                    </Grid>
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

1 个答案:

答案 0 :(得分:1)

如果没有明确说明问题的a good, minimal, complete code example,尝试提供有效的代码示例就不切实际了。

但我可以告诉你基本问题是什么:当用户点击ListBox中的实际项目时,该项目本身会处理用户输入。

您可以预览鼠标事件(请参阅例如Mouse.PreviewMouseDown Attached Event,密切注意记录的警告),或者您可以在列表项目本身中处理MouseDown事件。参见例如How can I get an event or command to fire when the user clicks on a ListViewItem?了解更多相关信息。