Reputation: 313
Why HandleListBoxClickEvent works only on the empty space of the list? The problem is when I want to refresh the list. While clicking on some item - nothing is updating. But clicking on the empty space of the list (when some item is selected) - selected item is updating.
I think, that some problem should be in xaml, because I was following a ready (working) example with the c# code.
Resources:
<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 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>
Upvotes: 0
Views: 273
Reputation: 70701
Without a good, minimal, complete code example that clearly illustrates the question, it's not practical to try to provide a working code example.
But I can tell you what the basic issue is: when the user clicks in an actual item in your ListBox
, the item itself handles the user input.
You can either preview the mouse event (see e.g. 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? for some more information regarding that.
Upvotes: 1