Question

I have trouble when I try to select some Item in DataGrid programmatically. Without using MVVM pattern all is OK. Look at XAML:

    <DataGrid 
        Name="_dataGrid"
        AutoGenerateColumns="False"
        SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>
            <DataGridTextColumn Binding="{Binding SecondName}"/>
        </DataGrid.Columns>
    </DataGrid>

Code behind:

    public class GridItem
    {
        public String Name { get; set; }
        public String SecondName { get; set; }
    }

    public partial class Window1 : Window
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private GridItem _selectedItem;
        public GridItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                RaisePropertyChanged("SelectedItem");
            }
        }

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            _dataGrid.Loaded += DataGridLoaded;
            Init1();
        }

        void DataGridLoaded(object sender, RoutedEventArgs e)
        {
            Int32 rowIndex = 2;
            var selItem = _dataGrid.Items[rowIndex];

            SelectedItem = (GridItem)selItem;     <-------- Bad
            //_dataGrid.SelectedItem = selItem;   <-------- Good
        }

        private void Init1()
        {
            var source = new List<GridItem>();
            source.Add(new GridItem
            {
                Name = "pavlik",
                SecondName = "bobr"
            });
            source.Add(new GridItem
            {
                Name = "alex",
                SecondName = "ugr"
            });
            source.Add(new GridItem
            {
                Name = "den",
                SecondName = "ivanov"
            });
            source.Add(new GridItem
            {
                Name = "dima",
                SecondName = "klim"
            });
            _dataGrid.ItemsSource = source;
        }
    }

So, when I select Item like that

_dataGrid.SelectedItem = selItem; // Good

Good result

Item is selected and highlighted properly.

But when I try to select and highlight Item via Model property, Item is not highlighted!

SelectedItem = (GridItem)selItem; // Bad

Bad result

What is the reason? Any idea?

Was it helpful?

Solution

you need to inherit your window or any viewmodel class from INotifyPropertyChanged otherwise it will not Notifies clients that a property value has changed.

for your case it could be like

public partial class Window1 : INotifyPropertyChanged
{
    // Class code goes here;
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

OTHER TIPS

I do this a lot, DataGrid to ViewModel, and I never have to do

SelectedItem = (GridItem)selItem;

i.e cast the item.

As you have SelectedItem on the DataGrid XAML, it knows the type you are binding too.

However, I normally use ItemSource on the gridview as well.

e.g. ItemSource = "{Bind the collection of GridItem}"

so in ViewModel, I would have Observablecollection or List as a property.

My WPF knowledge is only like 2 weeks old so I could be totally be wrong, but I think the cast and the Itemsource is where you need to look.

I could give you an example if you like.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top