Mandar Jogalekar
Mandar Jogalekar

Reputation: 3281

Wpf gridview selected Item

I have ListView with GridView inside view of ListView and ListView item source is specified. I dont seem to find how can. I get SelectedItem of GridView or SelectedItem changed.

<ListView Grid.Row="4" Margin="0,250,0,0" ItemsSource="{Binding TestBinding}" SelectedItem="{Binding Path=selectedItem}" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" SelectionChanged="ListView_SelectionChanged">
    <ListView.View>
        <GridView AllowsColumnReorder="False" >
            <GridViewColumn Header="Test" DisplayMemberBinding="{Binding Path=Test1}" Width="100" />
            <GridViewColumn Header="Test2" DisplayMemberBinding="{Binding Path=Test2}" Width="130" />                 
        </GridView>
    </ListView.View>
</ListView>

Upvotes: 3

Views: 7154

Answers (2)

James
James

Reputation: 303

Personally, I avoid observable collections 'cause I prefer more control over what my app does. So... In your XAML make sure your spelling is SelectedItem="{Binding Path=SelectedItem}", note uppercase S.

Then, in your SelectionChanged method, simply...

private void myListView_SelectionChanged(object sender,
   SelectionChangedEventArgs e)
{
   var selectedItem = (yourModel)myListView.SelectedItem;
}

Upvotes: 0

Wojciech Kulik
Wojciech Kulik

Reputation: 8450

Here is my code and it works fine:

public partial class MainWindow : Window, INotifyPropertyChanged, INotifyPropertyChanging
{
    public class MyObj
    {
        public string Test1 { get; set; }
        public string Test2 { get; set; }
    }

    public MainWindow()
    {
        InitializeComponent();

        TestBinding = new ObservableCollection<MyObj>();
        for (int i = 0; i < 5; i++)
        {
            TestBinding.Add(new MyObj() { Test1 = "sdasads", Test2 = "sdsasa" });
        }

        DataContext = this;
    }

    #region TestBinding

    private ObservableCollection<MyObj> _testBinding;

    public ObservableCollection<MyObj> TestBinding
    {
        get
        {
            return _testBinding;
        }
        set
        {
            if (_testBinding != value)
            {
                NotifyPropertyChanging("TestBinding");
                _testBinding = value;
                NotifyPropertyChanged("TestBinding");
            }
        }
    }
    #endregion

    #region selectedItem

    private MyObj _selectedItem;

    public MyObj selectedItem
    {
        get
        {
            return _selectedItem;
        }
        set
        {
            if (_selectedItem != value)
            {
                NotifyPropertyChanging("selectedItem");
                _selectedItem = value;
                NotifyPropertyChanged("selectedItem");
            }
        }
    }
    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify the page that a data context property changed
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    #region INotifyPropertyChanging Members

    public event PropertyChangingEventHandler PropertyChanging;

    // Used to notify the data context that a data context property is about to change
    protected void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    #endregion
}

Upvotes: 1

Related Questions