WPF HandleListBoxClickEvent只对列表的空白区域起作用
本文关键字:空白 区域 起作用 列表 HandleListBoxClickEvent WPF | 更新日期: 2023-09-27 18:01:37
为什么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>
ViewModel:
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>
如果没有一个好的,最小的, 完整的代码示例来清楚地说明这个问题,那么尝试提供一个工作代码示例是不实际的。
但是我可以告诉你基本问题是什么:当用户点击ListBox
中的实际项目时,该项目本身处理用户输入。
您可以预览鼠标事件(例如鼠标。PreviewMouseDown事件(请密切注意文档中的警告),或者您可以在列表项本身中处理MouseDown
事件。例如,当用户单击ListViewItem时,我如何获得一个事件或命令来触发?查看更多相关信息