We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Set SelectedItem by Code (MVVM)

How can i set the SelectedItem by Code in the ViewModel?

Have you any idea..

If i set the SelectedItem i got an error:

 <syncfusion:SfMultiColumnDropDownControl Grid.Row="1" Grid.Column="1" Height="22" Margin="5,4,0,5"  x:Name="MultiColumnControl"
                                                         VerticalAlignment="Center"
                                                         AllowAutoComplete="True"
                                                         AllowCaseSensitiveFiltering="False"
                                                         AllowImmediatePopup="True"
                                                         AllowIncrementalFiltering="True"
                                                         AutoGenerateColumns="False"
                                                         DisplayMember="CompanyName"
                                                         ItemsSource="{Binding AllCompanies}"
                                                         PopupWidth="400"
                                                         ReadOnly="False"
                                                         SelectedItem="{Binding SelectedCompany, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                         ValueMember="CompanyName">
            <syncfusion:SfMultiColumnDropDownControl.Columns>
                <syncfusion:GridTextColumn HeaderText="Id" MappingName="Id" IsHidden="True"/>
                <syncfusion:GridTextColumn Width="70" HeaderText="Uid"
                                                      MappingName="UidNumber"
                                                      TextAlignment="Center"
                                                      DisplayBinding="{Binding UidNumber}" />
                <syncfusion:GridTextColumn Width="120" HeaderText="Firma"
                                                      MappingName="CompanyName"
                                                      TextAlignment="Center"
                                                      DisplayBinding="{Binding CompanyName}" />
                <syncfusion:GridTextColumn Width="120" HeaderText="Straße"
                                                      MappingName="Street"
                                                      TextAlignment="Center"
                                                      DisplayBinding= "{Binding Street}" />
                <syncfusion:GridTextColumn Width="70" HeaderText="Postleitzahl"
                                                      MappingName="PostalCode"
                                                      TextAlignment="Center"
                                                      DisplayBinding= "{Binding PostalCode}"/>
                <syncfusion:GridTextColumn Width="100" HeaderText="Stadt"
                                                      MappingName="City"
                                                      TextAlignment="Center"
                                                      DisplayBinding= "{Binding City}"/>
                <syncfusion:GridTextColumn Width="100" HeaderText="Land"
                                                      MappingName="Country"
                                                      TextAlignment="Center"
                                                      DisplayBinding= "{Binding Country}"/>
            </syncfusion:SfMultiColumnDropDownControl.Columns>
        </syncfusion:SfMultiColumnDropDownControl>

MVVM Code:
 private Company _selectedCompany;

        public Company SelectedCompany
        {
            get { return _selectedCompany; }
            set
            {
                if (value == _selectedCompany)
                {
                    return;
                }
                _selectedCompany = value;
                if (value != null)
                {
                    if (SelectedOrder != null)
                    {
                        SelectedOrder.ClientData = _selectedCompany;
                    }
                }
                NotifyOfPropertyChange(() => SelectedCompany);
            }
        }

Thats the Error:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsBase.dll

   bei Syncfusion.UI.Xaml.Grid.SfMultiColumnDropDownControl.ProcessOnSelectedItemChanged(Object selectedItem)
   bei Syncfusion.UI.Xaml.Grid.SfMultiColumnDropDownControl.<.cctor>b__9(DependencyObject o, DependencyPropertyChangedEventArgs args)
   bei System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   bei System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   bei System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   bei System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   bei System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
   bei System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
   bei System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   bei System.Windows.Data.BindingExpression.Activate(Object item)
   bei System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
   bei System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
   bei MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
   bei MS.Internal.Data.DataBindEngine.Run(Object arg)
   bei MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
   bei System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   bei System.Windows.ContextLayoutManager.UpdateLayout()
   bei System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   bei System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   bei System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   bei System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   bei System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   bei System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   bei System.Windows.Interop.HwndTarget.OnResize()
   bei System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   bei System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

Thanks in advance

Many greetings from Germany
Wolfgang



3 Replies

SP Sowndaiyan Paulpandi Syncfusion Team December 7, 2015 11:33 AM UTC

Hi Feneberg,

Sorry for the delay caused.

We have analyzed your query with your provided code example. We are not able to reproduce the reported “NullReferenceException when set the SelectedItem in ViewModel”. For your reference we have attached the tested sample in the below sample location, please revert by modifying the attached sample based on your application along with the replication procedure. It will be helpful for us to analyze further. 


Sample Location: https://www.syncfusion.com/downloads/support/forum/121338/ze/MultiColumnDropDownDemo-1846307919

Regards,

Sowndaiyan



FE Feneberg February 15, 2016 07:57 PM UTC

Sorry for my late response.

The problem is fixed in the Version 13.4.0.53.

Thanks for the support.

Best regard.

Wolfgang


SP Sowndaiyan Paulpandi Syncfusion Team February 16, 2016 11:17 AM UTC

Hi Feneberg,

Thanks for the update.

Please let us know if you need further assistance.
Regards,
Sowndaiyan

Loader.
Live Chat Icon For mobile
Up arrow icon