简体   繁体   中英

Binding an Observable Collection of Objects to a RadTreeListView

as the Title states I just want to bind my ObservableCollection. I followed some only examples and looked at the telerik Website but seemingly Iam doing something wrong. Below is my DataContext as well as the Xaml code reduced to the necessary stuff. I you need more Ino iam happy to provide it. All o the Dog attributes have getter (and setter) and are o the datatypes Ushort, int and String.

While executing I get this Error note:

System.Windows.Data Error: 40: BindingExpression path error: 'bsdHeader' property not found on 'object' ''MainViewModel' (HashCode=19289328)'. BindingExpression:Path=dogs; DataItem='MainViewModel' (HashCode=19289328); target element is 'RadPaneGroup' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

EDIT: this Error disappeared after i added a get/set to the Observable collection dogs and moved the "new ObservableCollection" part into the Method.

public ObservableCollection<Dog> dogs = new ObservableCollection<Dog>();

  public void CreateList()
    {




        foreach(Dog dog in ListofDogs)
        {
            dogs.Add(dog);
        }

    }

.

<telerik:RadPaneGroup Grid.Row="3"  ItemsSource="{Binding Dog, UpdateSourceTrigger=PropertyChanged}">


    <telerik:RadPane Header="dogs" PaneHeaderVisibility="Hidden">
        <telerik:RadTreeListView>
            <telerik:RadTreeListView.ChildTableDefinitions>
                <telerik:TreeListViewTableDefinition ItemsSource="{Binding color}" />
                <telerik:TreeListViewTableDefinition ItemsSource="{Binding amountOfLegs}" />
                <telerik:TreeListViewTableDefinition ItemsSource="{Binding breed}" />
            </telerik:RadTreeListView.ChildTableDefinitions>
        </telerik:RadTreeListView>
    </telerik:RadPane>
</telerik:RadPaneGroup>

working xaml:

    <telerik:RadPaneGroup>
        <telerik:RadPane Header="Dogs" PaneHeaderVisibility="Hidden">
            <telerik:RadGridView x:Name="rgv_dogs" 
                                    ItemsSource="{Binding dogs, UpdateSourceTrigger=PropertyChanged}"  ShowGroupPanel="False" >
                <telerik:RadGridView.Columns >
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding color}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding amountOfLEgs}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding breed}"/>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </telerik:RadPane>
   </telerik:RadPaneGroup>

Okay, anticlimactic solution: My Visual Studio had an hickup? a collegue said maybe some update issue. After a reboot the code works.

I know nothing of Telerik UI, but I think you can try some options:

  1. ItemSource of telerik:RadPaneGroup is Dog in your code. It looks like Dog is type name, not the property name of ObservableCollection<Dog> .
  2. Is ObservableCollection<Dog> Dogs was declared in method? As far as I know, Property for ItemSource Binding of XAML Collections should be public Property of DataContext, except the case of binding happened in code behind.

I hope this can works with you too.

Edit: I was going to write 'can you change public ObservableCollection<Dog> dogs = new ObservableCollection<Dog>(); to public ObservableCollection<Dog> dogs{get;set;} = null; ?'. Because without getter and setter dogs is not considered as property. But it seems you already corrected that part.

Then If your telerik item still shows nothing, I recommend you to change CreateList back to declare it's own ObservableCollection<Dog> _dogs . But with the additional line like dogs = _dogs at the end, to force fire INotifyPropertyChanged of dogs . If this does not works too, You can see 'live visual tree' tool of Visual Studio and click the object to see it's ItemSource property. Maybe binding was happened well, but only not showing them visually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM