ListBox Code behind SetSelected called only when selecting item with right mouse button
Posted: 24 Jul 2020, 15:37
Hi,
I'm trying to make a ListBox containing some custom items with a ViewModel code-behind.
My code and XAML are quite similar to the QuestLog Noesis sample, but I got that very strange behaviour:
My property for selecting the current listbox item is called only when clicking on right mouse button, left mouse button does nothing. and I don't understand why.
Here is my listBox on XAML side :
And here is my ViewModel implementation (simplified) :
Where the ViewModel deelecration is (also simoplified) :
When i set a breakpoint in the SetSelectedLookTopLeft method, if I left click on a listbox item, nothing happens, if I right click on the item, it's selected and the breakpoint is reached.
What can I do to be able to select items on left mouse click ??
Thanks a lot,
Frank
I'm trying to make a ListBox containing some custom items with a ViewModel code-behind.
My code and XAML are quite similar to the QuestLog Noesis sample, but I got that very strange behaviour:
My property for selecting the current listbox item is called only when clicking on right mouse button, left mouse button does nothing. and I don't understand why.
Here is my listBox on XAML side :
Code: Select all
<ControlTemplate x:Key="Car4LooksItemTemplate" TargetType="{x:Type ListBoxItem}">
<StackPanel Orientation="Vertical">
<Button Width="130" Height="137" Foreground="Red" BorderBrush="Red" Background="#80F2F2F2" Opacity="1.0" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Width="116" Height="134" Background="{StaticResource BandeauColor}" Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Canvas Width="85" Height="130" Background="{StaticResource BandeauColor}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Rectangle Width="14" Height="14" Canvas.Left="-25" Fill="{Binding DefaultColor}" Stroke="{Binding DefaultColor}" StrokeThickness="0" Margin="90,19,0,0" />
<Viewbox Width="85" Height="95" Canvas.Top="17">
<Image Source="{Binding Image}" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="270"/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
</Viewbox>
</Canvas>
<TextBlock Text="{Binding Name}" TextAlignment="Center" Width="100" Height="12" Margin="-40,0,0,0" FontSize="10" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF000000" Background="#00000000" FontWeight="Bold" TextTrimming="CharacterEllipsis">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="270"/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Text="{Binding Number}" TextAlignment="Center" Width="100" Height="13" Margin="-85,0,0,0" FontSize="11" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF808080" Background="#00000000" FontWeight="Bold">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="270"/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
</StackPanel>
</Button>
<Rectangle Fill="Black" Margin="0,0,0,0" Stroke="Black" Width="70" Height="2" HorizontalAlignment="Center" />
</StackPanel>
</ControlTemplate>
<Style x:Key="Car4LooksItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template" Value="{StaticResource Car4LooksItemTemplate}"/>
</Style>
<ControlTemplate x:Key="Template.ListBox" TargetType="ListBox">
<ScrollViewer Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Focusable="False">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
<Style x:Key="Car4LooksListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Single"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="VirtualizingPanel.VirtualizationMode" Value="Recycling"/>
<Setter Property="VirtualizingPanel.ScrollUnit" Value="Pixel"/>
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Contained"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template" Value="{StaticResource Template.ListBox}"/>
</Style>
(...)
<ListBox x:Name="TopLeftMakeupSelector"
Style="{StaticResource Car4LooksListStyle}"
ItemContainerStyle="{StaticResource Car4LooksItemStyle}"
ItemsSource="{Binding LooksTopLeft}"
SelectedItem="{Binding SelectedLookTopLeft}"
DisplayMemberPath="Name"
Height="540"
Width="136">
</ListBox>
Code: Select all
////////////////////////////////////////////////////////////////////////////////////////////////////
struct ViewModel::Look : public BaseComponent
{
public:
String Name;
Ptr<ImageSource> Image;
String Number;
String Description;
Ptr<Brush> DefaultColor;
private:
NS_IMPLEMENT_INLINE_REFLECTION(Look, BaseComponent)
{
NsProp("Name", &Look::Name);
NsProp("Image", &Look::Image);
NsProp("Number", &Look::Number);
NsProp("Description", &Look::Description);
NsProp("DefaultColor", &Look::DefaultColor);
}
};
ViewModel::ViewModel()
{
_LooksTopLeft = *new ObservableCollection<Look>();
Ptr<ImageSource> image0 = *new BitmapImage("5eac6aeea60a650017b21177-compressed.png");
Ptr<ImageSource> image1 = *new BitmapImage("5eac6b63a60a650017b211af-compressed.png");
Ptr<Brush> color0 = *new SolidColorBrush(Color::Red);
Ptr<Brush> color1 = *new SolidColorBrush(Color::Green);
{
Ptr<Look> q = *new Look();
q->Name = "EYES TO KILL";
q->Image = image0;
q->Description = "Ldolor ac leo convallis";
q->Number = "12";
q->DefaultColor = color0;
_LooksTopLeft->Add(q);
}
{
Ptr<Look> q = *new Look();
q->Name = "NEO NUDE BLUSH";
q->Image = image1;
q->Description = "Nullam volutpat felis";
q->Number = "26";
q->DefaultColor = color1;
_LooksTopLeft->Add(q);
}
_selectedLookTopLeft = _LooksTopLeft->Get(0);
}
(...)
////////////////////////////////////////////////////////////////////////////////////////////////////
void ViewModel::SetSelectedLookTopLeft(Look* value)
{
if (_selectedLookTopLeft != value)
{
_selectedLookTopLeft = value;
OnPropertyChanged("SelectedLookTopLeft");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ViewModel::Look* ViewModel::GetSelectedLookTopLeft() const
{
return _selectedLookTopLeft;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
NS_BEGIN_COLD_REGION
NS_IMPLEMENT_REFLECTION(ViewModel)
{
NsProp("LooksTopLeft", &ViewModel::_LooksTopLeft);
NsProp("SelectedLookTopLeft", &ViewModel::GetSelectedLookTopLeft, &ViewModel::SetSelectedLookTopLeft);
}
Code: Select all
class ViewModel : public NoesisApp::NotifyPropertyChangedBase
{
public:
ViewModel();
private:
struct Look;
void SetSelectedLookTopLeft(Look* value);
Look* GetSelectedLookTopLeft() const;
Noesis::Ptr<Noesis::ObservableCollection<Look>> _LooksTopLeft;
Look* _selectedLookTopLeft;
NS_DECLARE_REFLECTION(ViewModel, NotifyPropertyChangedBase)
};
What can I do to be able to select items on left mouse click ??
Thanks a lot,
Frank