0
votes

I'm tryng to bind the selectedItem of a datagrid with a property in MVVM. the problem is that it does not fire the "Set" of the property.

in the xaml part i have:

 <WPFCtrlDg:ExtDataGrid Name="_edgMessage" Grid.Row="1" 
      ItemsSource="{Binding Path=LNE_MESSAGE, Mode=OneWay}"
      SelectedItem="{Binding Path=CurrentMessage, Mode=TwoWay}">

in the code part:

 private LNE_MESSAGE _currentMessage;
    public LNE_MESSAGE CurrentMessage
    {
        get
        {
            if (_currentMessage == null)
            {
                ICollectionView collectionView = CollectionViewSource.GetDefaultView(LNE_MESSAGE);
                if (collectionView != null)
                    return collectionView.CurrentItem as LNE_MESSAGE;
                return null;
            }
            return _currentMessage;
        }
        set
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(LNE_MESSAGE);
            if (collectionView != null)
                collectionView.MoveCurrentTo(value);

            _currentMessage = value;
            OnPropertyChanged(() => CurrentMessage);
        }
    }

the extdatagrid is a custom control and the selected item property is done this way:

        public object SelectedItem
    {
        get { return GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(object), typeof(ExtDataGrid),
                                new UIPropertyMetadata((s, e) => 
                                                                {
                                                                    ExtDataGrid extDg = s as ExtDataGrid;
                                                                    Debug.Assert(extDg != null);
                                                                    extDg.CurrentItem = e.NewValue;
                                                                }));

any idea of how to bind correctly the selecteditem property?

1

1 Answers

0
votes

Check if your datacontext is being set right. And debug and see locals window to see, if datacontext is actually being set by using the visual helper. Also see output window for any binding errors.

At first sight your dependency property looks right.