WPF HandleListBoxClickEvent 仅适用于列表的空 space
WPF HandleListBoxClickEvent works only on the empty space of the list
为什么 HandleListBoxClickEvent 仅对列表的空 space 有效?问题是当我想刷新列表时。单击某些项目时 - 没有任何更新。但是单击列表的空 space(当某些项目被选中时)- 所选项目正在更新。
我认为 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 列表:
<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>
没有 a good, minimal, complete code example 清楚地说明问题,尝试提供工作代码示例是不切实际的。
但我可以告诉您基本问题是什么:当用户单击您 ListBox
中的实际项目时,该项目本身会处理用户输入。
您可以预览鼠标事件(参见 Mouse.PreviewMouseDown Attached Event, paying close attention to the documented caveats), or you can handle the MouseDown
event in the list item itself. See e.g. How can I get an event or command to fire when the user clicks on a ListViewItem? 了解更多相关信息。
为什么 HandleListBoxClickEvent 仅对列表的空 space 有效?问题是当我想刷新列表时。单击某些项目时 - 没有任何更新。但是单击列表的空 space(当某些项目被选中时)- 所选项目正在更新。
我认为 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 列表:
<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>
没有 a good, minimal, complete code example 清楚地说明问题,尝试提供工作代码示例是不切实际的。
但我可以告诉您基本问题是什么:当用户单击您 ListBox
中的实际项目时,该项目本身会处理用户输入。
您可以预览鼠标事件(参见 Mouse.PreviewMouseDown Attached Event, paying close attention to the documented caveats), or you can handle the MouseDown
event in the list item itself. See e.g. How can I get an event or command to fire when the user clicks on a ListViewItem? 了解更多相关信息。