0

Can someone help me with the list view with grid. I want when you click on an element that a function is called and I get the current item. I already have the following XAML code:

<ListView Name="lstView" ItemsSource="{Binding Path=SimResults}" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem, ElementName=lstView}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="FileUniqueID" Width="Auto" DisplayMemberBinding="{Binding Path=FileUniqueID}" />
                <GridViewColumn Header="XML" Width="Auto" DisplayMemberBinding="{Binding Path=XML}" />
                <GridViewColumn Header="Request" Width="Auto" HeaderStringFormat="" DisplayMemberBinding="{Binding Path=RequestShort}" />
                <GridViewColumn Header="RequestDate" Width="Auto" DisplayMemberBinding="{Binding Path=RequestDate}" />
                <GridViewColumn Header="Response" Width="Auto" DisplayMemberBinding="{Binding Path=ResponseShort}" />
                <GridViewColumn Header="ResponseDate" Width="Auto" DisplayMemberBinding="{Binding Path=ResponseDate}" />
                <GridViewColumn Header="ResendCounter" Width="Auto" DisplayMemberBinding="{Binding Path=ResendCounter}" />
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

In the cs file I have the following functions:

private Item selectedItem;
private RelayCommand selectedItemCommand;
this.selectedItemCommand = new RelayCommand(this.SelectedItems);

        public Item SelectedItem
        {
            get { return selectedItem; }
            set { selectedItem = value; OnPropertyChanged("SelectedTrends"); }
        }

        public RelayCommand SelectedItemCommand
        {
            get
            {
                return this.selectedItemCommand;
            }

        }

        private void SelectedItems(object obj)
        {
            this.requestXml = this.selectedItem.DisplayName;
        }

When I select an element the selectedItems and I can get the item.

Hi I tested the code above and it worked fine. But I have problem. I have different tabs, when I click a different tab with the left click it always runs the SelectionChanged event and I get a Null reference exception.

2 Answers 2

1

One solution is to create a property for the "SelectedItem" from the listView.

So, in your VM:

private YourType _selectedResult;
public YourType SelectedResult
{
    get { return _selectedResult; }
    set { _selectedResult= value; OnPropertyChanged("SelectedResult"); }
}

And bind this proprety on your ListView:

<ListView Name="lstView" ItemsSource="{Binding Path=SimResults}" SelectedItem="{Binding SelectedResult}">
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, thanks for your reply now the code above works but the SelectionChanged event also runs when I change to a different tab and I get a Null reference. Do you know how I can accomplish the the event is only accepted for a click on an item on the list view.
You don't need the SelectionChanged Event anymore. When you select another element in your ListView, the setter of SelectedResult is executed. (Maybe you have to specify the "Mode=TwoWay" in your binding in SelectedItem)
Hi thanks for you reply. If I debugged the code and select an item in the listview with left click the getter and setter isn't called I updated my current code in the main post.
Maybe can you also describe me how I can get things work with my first aproach, as now the SelectionChanged event runs when I change to a different tab and I get a Null reference. The event should be only accepted when you make a click on an item on the list view, so I can do it with Command and Relay Classes.
Did you try the event "ItemSelectionChanged" ?
|
1

Just make sure you initialize selectedItemCommand before InitializeComponent() in the ctor, eg:

public MainWindow()
{
    this.selectedItemCommand = new RelayCommand(this.SelectedItems);

    InitializeComponent();
}

2 Comments

Hi, thanks for your reply now the code above works but the SelectionChanged event also runs when I change to a different tab and I get a Null reference. Do you know how I can accomplish the the event is only accepted for a click on an item on the list view.
@user2644964: what if you modify SelectedItems to return immediately in case a null is received in the parameter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.