This is a migrated thread and some comments may be shown as answers.

SelectedItem binding issue

7 Answers 522 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Felix
Top achievements
Rank 1
Felix asked on 01 Oct 2020, 03:16 PM

Hello,

We are using RadListBox as part of UserControl. Control consists of couple of data templates one of which contains list box:

<telerik:RadListBox Grid.Row="2" x:Name="DocumentListBox" ItemsSource="{Binding ItemsView}" BorderThickness="0"
            ItemTemplateSelector="{Binding TemplateSelector}"
            SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            Background="Transparent"
            HorizontalContentAlignment="Stretch"
            SelectionMode="Single"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled"
            ScrollViewer.IsDeferredScrollingEnabled="{Binding IsDeferredScrollingEnabled}">
    <telerik:EventToCommandBehavior.EventBindings>
        <telerik:EventBinding EventName="MouseDoubleClick" Command="{Binding OpenItemCommand}" CommandParameter="{Binding SelectedItem}"/>
    </telerik:EventToCommandBehavior.EventBindings>
    <telerik:RadListBox.ItemContainerStyle>
        <Style TargetType="telerik:RadListBoxItem" BasedOn="{StaticResource {x:Type telerik:RadListBoxItem}}">
            <EventSetter Event="MouseMove" Handler="DocumentListBox_OnMouseMove"/>
        </Style>
    </telerik:RadListBox.ItemContainerStyle>
    <telerik:RadListBox.InputBindings>
        <KeyBinding Key="Delete" Command="{Binding DeleteItemCommand}" CommandParameter="{Binding SelectedItem}"/>
        <KeyBinding Modifiers="Control" Key="O" Command="{Binding OpenItemCommand}" CommandParameter="{Binding SelectedItem}"/>
    </telerik:RadListBox.InputBindings>
</telerik:RadListBox>

 

Control always used with the same view model. Most of the times SelectedItem binding works fine. But in two places it doesn't.

There is no binding related errors in the output, RadListBox is correctly populated with items and RadListBox.SelectedItem has correct value.

But in this two problematic cases SelectedItem property of the view model does not get updated.

We suspect that the problem might have something to do with the initialization order, but could not detect any specific pattern.

In one case the control used in the tab in the other in dialog window.

7 Answers, 1 is accepted

Sort by
0
Felix
Top achievements
Rank 1
answered on 01 Oct 2020, 04:05 PM

I've investigated further and found interesting thing.

I've executed this test code in the mouse click event handler:

var tmp = (Telerik.Windows.Controls.RadListBoxItem) sender;
var listBox = (Telerik.Windows.Controls.RadListBox)tmp.ParentSelector;
BindingExpression bindingExpression = listBox.GetBindingExpression(Telerik.Windows.Controls.RadListBox.SelectedItemProperty);
if (bindingExpression is null)
{
    Binding myBinding = new Binding();
    myBinding.Source = DataContext;
    myBinding.Path = new PropertyPath("SelectedItem");
    myBinding.Mode = BindingMode.TwoWay;
    myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(listBox, Telerik.Windows.Controls.RadListBox.SelectedItemProperty, myBinding);
}

 

When I executed this code in places where our control does not work property (SelectedItem property of the view model is not updated) bindingExpression was null. In other cases it isn't.

And after missing binding is re-created it works just fine.

We does not access RadListBox from code behind and does not set anything to its SelectedItem so I don't see how binding could be overwritten from our side.

0
Felix
Top achievements
Rank 1
answered on 02 Oct 2020, 08:20 AM

Potentially useful information:

We are binding ItemSource of the RadListBox to the View of the CollectionViewSource.

0
Dilyan Traykov
Telerik team
answered on 05 Oct 2020, 10:18 AM

Hello Frank,

Thank you for the provided code snippets and details.

I also assume that this is a timing issue related to the bindings, but without being able to replicate it at my end, I cannot be sure of its exact cause. Would you find it possible to open a new support ticket and provide a small sample project which demonstrates the binding issue? Alternatively, you can upload the project to a storage provider of your choice and send over a download link. Please make sure to exclude all Telerik binaries from the project before this, however.

In the meantime, can you also specify how and where the DataContext for this RadListBox is set? Please also try to replace the RadListBox with a native ListBox control and let me know whether the issue is still observed.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Felix
Top achievements
Rank 1
answered on 06 Oct 2020, 07:50 AM

Hello Dilyan,

 

Thank you for the response.

Xaml I added to the first post used inside the data template which is used like this: 

<ContentControl Content="{Binding}" ContentTemplate="{StaticResource OverviewTemplate}"/>

 

That is how we set data context of the window:

new Window { Title = title, DataContext = dataContext, Content = content }

 

It is displayed using ShowDialog.

 

We tried to replace RadListBox with the native ListBox. The binding works in all situations where we have an issue with RadListBox.

 

I'll start working on minimal working example which could reproduce the issue and provide it as soon as it is ready.

0
Felix
Top achievements
Rank 1
answered on 06 Oct 2020, 03:39 PM

It appeared that the key to reproduce the issue was to put RadListBox into RadTabItem of the RadTabControl.

Here is a small solution which reproduces the issue:

https://drive.google.com/file/d/10gEA7e4iliZ80goBDNJnjaAHs8jS5mvY/view?usp=sharing

0
Dilyan Traykov
Telerik team
answered on 08 Oct 2020, 03:40 PM

Hi Frank,

Thank you very much for the provided.

There appears to be some timing issue with the SelectedItem binding with this particular setup. What you can do to avoid it is to set the binding after the control has fully loaded, like so:

        private void DocumentListBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            var lb = sender as Telerik.Windows.Controls.RadListBox;
            var dc = lb.DataContext as CollectionOverviewViewModel;
            lb.SetBinding(Telerik.Windows.Controls.RadListBox.SelectedItemProperty, new Binding("SelectedItem") { Source = dc, Mode = BindingMode.TwoWay });
        }
Please give this a try and let me know if it successfully resolves the binding at your end.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Felix
Top achievements
Rank 1
answered on 12 Oct 2020, 01:16 PM

Hi Dilyan,

 

The workaround works, thank you.

Tags
ListBox
Asked by
Felix
Top achievements
Rank 1
Answers by
Felix
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or