Reputation: 25
I am binding a ListBox
to a List
in the ViewModel named FilmeSerienListe
. SelectedItem
is set when ismouseover = True
. The SelectedItem
of the ListBox is bound to a Property in the ViewModel named SelectedFilmSerie
.
This means when the mouse is over a particular ListBoxItem, this ListBoxItem is selected and its value bound to SelectedFilmSerie
. BUT this does not seem to work very well, because the SelectedFilmSerie
Property is for some reason always NULL
. So, I debugged the App to see what did go wrong – Now the weird behaviour – SelectedFilmSerie
is in fact NOT the WHOLE time NULL.
At first – when the mouse is over a ListBoxItem – the value
parameter is set to the right Object, and it is NOT NULL, BUT then when I go further with my debugging this SelectedFilmSerie
Property is recalled and NOW the value parameter is NULL, which make the SelectedFilmSerie
Property also NULL.
WPF:
<ListBox ItemsSource="{Binding FilmeSerienListe}"
SelectedItem="{Binding SelectedFilmSerie}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="IsSelected"
Value="true" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
C#
private IEnumerable<FilmeSerien> _filmeSerienListe;
public IEnumerable<FilmeSerien> FilmeSerienListe
{
get => _filmeSerienListe;
set
{
_filmeSerienListe = value;
NotifyPropertyChanged();
}
}
private FilmeSerien _selectedFilmSerie;
public FilmeSerien SelectedFilmSerie
{
get => _selectedFilmSerie;
set
{
Trace.WriteLine(value != null ? "Value is NOT null" : "Value is NULL");
_selectedFilmSerie = value;
NotifyPropertyChanged();
}
}
Debugger:
ismouseover = True:
4xStep Over:
1xStep Over:
4xStep Over:
Output:
The sec the mouse entered the ListBoxItem, I get Value is NOT null
and the sec the mouse leaves the ListBoxItem, I get Value is NULL
More details:
The ListBox is a list of cards bound to FilmeSerienListe
with the datatype IEnumerable<FilmeSerien>
each ListBoxItem
is a card which is a representation of a FilmeSerien
.
A card contains several Properties that are in a Property with the datatype FilmeSerien
. Like: Titel, Img, Vote and other. Vote
is an int
datatype Property.
Looks something like this:
Problem:
Every Time I hit a star to vote a movie. I get a NullReferenceException because the SelectedFilmSerie
is null.
WPF For star:
<Viewbox>
<materialDesign:RatingBar Value="{Binding Vote}"
Orientation="Horizontal"
Foreground="SkyBlue"/>
</Viewbox>
Upvotes: 1
Views: 152
Reputation: 7908
If I understand you correctly, you need to select the ListBox element not by clicking, but by hovering the mouse over it.
To be honest, it does not even occur to my head for what this might be needed.
I asked in the comments to clarify the purpose of such an implementation, but you didn't answer.
But if, I understood your question correctly, then it is implemented like this:
<Window.Resources>
<Style x:Key="listBoxItemStyle" TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid VerticalAlignment="Center" >
<ListBox ItemsSource="{Binding FilmeSerienListe}"
SelectedItem="{Binding SelectedFilmSerie}"
ItemContainerStyle="{DynamicResource listBoxItemStyle}">
</ListBox>
</Grid>
Upvotes: 3