Trap Error on SelectedItem change

3 posts, 0 answers
  1. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 29 Jun 2015 Link to this post

    I have the SelectedItem of my TreeListView bound to a property.  However, if conditions aren't met that Property will throw an exception.  How do I capture that exception?  Is it a Binding error?  Is it a Data error?  How do I trap and expose it?  DataError and GiveFeedback events don't seem to give me anything to trap.

     

    <telerik:RadTreeListView
        x:Name="treeListView"
        AutoGenerateColumns="False"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        CanUserFreezeColumns="False"
        CanUserReorderColumns="False"
        SelectionMode="Single"
        Grid.Row="1"
        Margin="5"
        IsReadOnly="{Binding IsReadOnly}"
        ItemsSource="{Binding Hierarchy, Mode=TwoWay}"
        SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
        AutoExpandItems="True"
        ShowGroupPanel="False"
        IsFilteringAllowed="False"
        ShowColumnHeaders="False"
        IsExpandedBinding="{Binding IsExpanded, Mode=TwoWay}" DataError="treeListView_DataError" GiveFeedback="treeListView_GiveFeedback">
     
        <telerik:RadTreeListView.ChildTableDefinitions>
            <telerik:TreeListViewTableDefinition 
                ItemsSource="{Binding Children}"/>
        </telerik:RadTreeListView.ChildTableDefinitions>
        <telerik:RadTreeListView.Columns>
     
            <telerik:GridViewDataColumn
                DataMemberBinding="{Binding Name, Mode=TwoWay}"
                CellTemplate="{StaticResource TreeNodeTemplate}"
                IsReadOnly="False"
                Width="*"/>
     
            <telerik:GridViewComboBoxColumn
                x:Name="itemTypeColumn"
                Header="Item Type"
                DataMemberBinding="{Binding ItemTypeID, Mode=TwoWay}"
                DisplayMemberPath="Name"
                SelectedValueMemberPath="ID"
                SortMemberPath="Order"
                SortingState="Ascending"
                ItemsSource="{Binding TypeModel.Items, Mode=TwoWay}"
                IsReadOnly="True"
                Width="150"/>
     
            <telerik:GridViewDataColumn
                DataMemberBinding="{Binding IsActive, Mode=TwoWay}"
                Header="Include"/>
     
        </telerik:RadTreeListView.Columns>
    </telerik:RadTreeListView>

  2. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 29 Jun 2015 Link to this post

    To provide more context, I have a MainWindow --> UserControl --> TreeListView.  The MainWindow has a ModelView DataContext that has several sub-ModelViews.  So, the DataContext glues multiple UserControls together. 
    When I select an item on the TreeListView control, the Property fires a "PropertyValueChanging" event where I perform a Save on another control in the UI.  Then, the property value is updated and I fire a "PropertyValueChanged" event where I then perform a Load on another control in the UI.

    Problem is, if an exception is thrown in the PropertyValueChanging event handler, I want to expose the exception to the user and I want the TreeListView to stay on the "old" item; not the just-clicked item.  I have figured out how to expose the error to the user but I don't know how to prevent the TreeListView from moving to the newly selected node.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 02 Jul 2015 Link to this post

    Hello,

    As far as I can understand, you perform the validation logic in the ViewModel. So that, RadTreeListView's SelectedItem property is updated first and then the bound property may throw an exception. Thus you need to process the exception and set the valid value to the SelectedItem property manually. Is it possible for you to carry out this validation in RadTreeListView's SelectionChaning event, so that you can prevent the SelectedItem property from changing its value if it is an invalid one?

    Regards,
    Ivan Ivanov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top