Ivana
Ivana

Reputation: 9

Why my SelectedItem from ListView isn't working?

This is code in wpf which shows the tour requests, arranged in two columns:

<ListView x:Name="TourRequestsListView" ItemsSource="{Binding TourRequests}" SelectedItem="{Binding SelectedTourRequest, Mode=TwoWay}" Background="Transparent" BorderThickness="0" HorizontalAlignment="Center" Margin="10">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="10">
                <Border BorderBrush="White" Background="#05085B" BorderThickness="2" CornerRadius="10" Width="500" Height="225">
                    <StackPanel>
                        <Border Background="{Binding State, Converter={StaticResource StateToBackgroundConverter}}" Margin="25,20,0,0" BorderBrush="White" HorizontalAlignment="Left" BorderThickness="2" CornerRadius="10" Width="250">
                            <DockPanel>
                                <TextBlock Text="Status: " Margin="15,5,0,5" FontSize="18" FontWeight="Bold" FontFamily="Helvetica" Foreground="White"/>
                                <TextBlock Text="{Binding State}" Margin="0,5,5,5" FontSize="18" FontWeight="Bold" FontFamily="Helvetica" Foreground="White"/>
                            </DockPanel>
                        </Border>
                        <DockPanel>
                            <Image Source="/wpf/icons/touristicons/schedule.png" Margin="25,20,0,0" Width="30" VerticalAlignment="Center"/>
                            <TextBlock FontSize="18" FontFamily="Helvetica" Foreground="White" Margin="5,20,0,0" VerticalAlignment="Center">
                                <Run Text="Creation Date: " />
                                <Run Text="{Binding CreationDate}" />
                            </TextBlock>
                        </DockPanel>
                        <DockPanel>
                            <Image Source="/wpf/icons/touristicons/number-blocks.png" Margin="25,20,0,0" Width="30" VerticalAlignment="Center"/>
                            <TextBlock FontSize="18" FontFamily="Helvetica" Foreground="White" Margin="5,20,0,0" VerticalAlignment="Center">
                                <Run Text="Number of simple tour requests: " />
                                <Run Text="{Binding NumberOfRequests}" />
                            </TextBlock>
                        </DockPanel>
                        <Button Content="See Details" Margin="0,20,0,0" Command="{Binding ElementName=TourRequestsListView, Path=DataContext.SeeDetailsCommand}" CommandParameter="{Binding}" HorizontalAlignment="Center" BorderBrush="Transparent" Background="Transparent" VerticalAlignment="Center">
                            <Button.Template>
                                <ControlTemplate TargetType="Button">
                                    <TextBlock Text="{TemplateBinding Content}" TextDecorations="Underline" Cursor="Hand" FontFamily="Helvetica" FontSize="20" Foreground="Yellow"/>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                    </StackPanel>
                </Border>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ItemsSource is binded to TourRequests and SelectedItem to SelectedTourRequest C# (DataContext set to this viewModel):

private ObservableCollection<ComplexTourRequest> _tourRequests;
public ObservableCollection<ComplexTourRequest> TourRequests
{
    get
    { return _tourRequests; }
    set
    {
        _tourRequests = value;
        OnPropertyChanged("TourRequests");
    }
}
private ComplexTourRequest _selectedTourRequest;
public ComplexTourRequest SelectedTourRequest
{
    get
    { return _selectedTourRequest; }
    set
    {
        _selectedTourRequest = value;
        OnPropertyChanged("SelectedTourRequest");
    }
}

viewModel extends class with propertyChanged implementation: public class ReviewComplexTourRequestsViewModel : TouristViewModel

TouristViewModel.cs:

public class TouristViewModel : INotifyPropertyChanged
{
    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

When I debug, when clicking on an element from the list, only the get method is called, but not the set method, and selectedRourRequest returns null

Upvotes: 0

Views: 57

Answers (0)

Related Questions