Binding to Collection

5 posts, 0 answers
  1. Kenneth
    Kenneth avatar
    70 posts
    Member since:
    Feb 2008

    Posted 16 Sep 2009 Link to this post

    When I expand the column, to view the child table, I get the error: A TwoWay or OneWayToSource binding cannot work on the read-only property 'AddressFamily' of type 'System.Net.IPEndPoint'.

    I had the same error on the parent table - but got around it by explicitly defining the columns and the column bindings as one-way. Not really sure why that was required either, but it worked. But, can't figure out how to do that for the child table. And, I'm not sure if this was the correct approach anyway. 

    I get this scenario to work correctly with DataSets - but don't want to have to create DataSets for the  50+ objects I need to do this for.

    Any thoughts? 

    The datasource is a collection of custom objects.

    XAML is...
    <Window x:Class="WpfApplication1.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" Height="313" Width="420" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" WindowStyle="ThreeDBorderWindow" SizeToContent="Manual"
        <Grid> 
            <Grid.Resources> 
            </Grid.Resources> 
            <telerik:RadGridView  
                Name="radGridView1"  
                AllowDrop="False"  
                ColumnsWidthMode="Auto"  
                AutoGenerateColumns="False" > 
                <telerik:RadGridView.Columns> 
                    <telerik:GridViewDataColumn    
                        HeaderText="Name"   
                        DataMemberBinding="{Binding Path=Name, Mode=OneWay}"/> 
                    <telerik:GridViewDataColumn   
                        HeaderText="HostName"   
                        DataMemberBinding="{Binding Path=HostName, Mode=OneWay}" /> 
                    <telerik:GridViewDataColumn   
                        HeaderText="Type"   
                        DataMemberBinding="{Binding Path=Type, Mode=OneWay}" /> 
                    <telerik:GridViewDataColumn   
                        HeaderText="Domain" 
                        DataMemberBinding="{Binding Path=Domain, Mode=OneWay}" /> 
                </telerik:RadGridView.Columns> 
            </telerik:RadGridView> 
        </Grid> 
    </Window> 

    code behind is:
        public partial class Window1 : Window 
        { 
            Browser browser; 
            BindingList<OtogramService> servicesBindingList = new BindingList<OtogramService>(); 
     
            public Window1() 
            { 
                InitializeComponent(); 
     
                // Create the browser 
                browser = new Browser("_http._tcp"); 
     
                // Hook the Updated event. 
                browser.Updated += new Browser.EventHandler(browser_Updated); 
     
                //create the relationship   
                var detailDefinition = new Telerik.Windows.Controls.GridViewTableDefinition(); 
                detailDefinition.Relation = new Telerik.Windows.Data.PropertyRelation("Addresses"); 
                radGridView1.TableDefinition.ChildTableDefinitions.Add(detailDefinition); 
     
                radGridView1.ItemsSource = servicesBindingList; 
            } 
     
            void browser_Updated2(Browser sender, OtogramBrowserEventArgs e) 
            { 
                switch (e.otogramBrowserEvent) 
                { 
                    // note - it is possible to get an Updated* event 
                    // before the Add event. 
                    case OtogramService.Event.UpdatedAddress: 
                    case OtogramService.Event.UpdatedTextRecord: 
                    case OtogramService.Event.Add: 
                        { 
                            try 
                            { 
                                // if we have one just update it 
                                var z = (from p in servicesBindingList where p.Name == e.otogramService.Name select p).Single(); 
                                z = e.otogramService; 
                            } 
                            catch 
                            { 
     
                                // if we don't have one already - add a new one. 
                                servicesBindingList.Add(e.otogramService); 
                            } 
     
                        } 
                        break
     
                    case OtogramService.Event.Remove: 
                        { 
                            // if we have one - remove it. 
                            var z = from p in servicesBindingList where p.Name == e.otogramService.Name select p; 
                            if (z.Count() != 0) 
                            { 
                                servicesBindingList.Remove(z.First()); 
                            } 
                        } 
                        break
                } 
            } 
     
            private delegate void UpdateBrowser(Browser sender, OtogramBrowserEventArgs e); 
     
            // required because WPF must execute one on one thread.  
            // This puts the callback into the WPF thread's dispatch queue. 
            void browser_Updated(Browser sender, OtogramBrowserEventArgs e) 
            { 
                UpdateBrowser func = browser_Updated2; 
                this.Dispatcher.BeginInvoke(func, new object[] { sender, e }); 
            } 
        } 
     

  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 16 Sep 2009 Link to this post

    Hi Kenneth,

    You can easily define columns for child tables and the child table itself using HierarchyChildTemplate property. Here is an example:

    <telerik:RadGridView 
                Name="radGridView1" 
                AllowDrop="False" 
                ColumnsWidthMode="Auto" 
                AutoGenerateColumns="False">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn   
                        HeaderText="Name"  
                        DataMemberBinding="{Binding Path=Name, Mode=OneWay}"/>
                    <telerik:GridViewDataColumn  
                        HeaderText="HostName"  
                        DataMemberBinding="{Binding Path=HostName, Mode=OneWay}" />
                    <telerik:GridViewDataColumn  
                        HeaderText="Type"  
                        DataMemberBinding="{Binding Path=Type, Mode=OneWay}" />
                    <telerik:GridViewDataColumn  
                        HeaderText="Domain"
                        DataMemberBinding="{Binding Path=Domain, Mode=OneWay}" />
                </telerik:RadGridView.Columns>
               <telerik:RadGridView.HierarchyChildTemplate>
                   <DataTemplate>
                        <telerik:GridViewDataControl>
                             <telerik:
    GridViewDataControl.Columns>
                                      ...
                             </telerik:
    GridViewDataControl.Columns>
                        </telerik:
    GridViewDataControl>
                   </DataTemplate>
               </telerik:RadGridView.HierarchyChildTemplate>

            </telerik:RadGridView>

    Sincerely yours,
    Vlad
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Kenneth
    Kenneth avatar
    70 posts
    Member since:
    Feb 2008

    Posted 16 Sep 2009 Link to this post

    Thanks for the quick response. I had experimented with that before but couldn't get it to work. Here's what I did. I think the problem may be with the Path statement. The parent row collection field is: Addresses. And the detal field I want to display is "Address".

    Here's the new XAML.

    <Window x:Class="WpfApplication1.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" Height="313" Width="420" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" WindowStyle="ThreeDBorderWindow" SizeToContent="Manual"
        <Grid> 
            <Grid.Resources> 
            </Grid.Resources> 
            <telerik:RadGridView  
                Name="radGridView1"  
                AllowDrop="False"  
                ColumnsWidthMode="Auto"  
                AutoGenerateColumns="False" > 
                <telerik:RadGridView.Columns> 
                    <telerik:GridViewDataColumn    
                        HeaderText="Name"   
                        DataMemberBinding="{Binding Path=Name, Mode=OneWay}"/> 
                    <telerik:GridViewDataColumn   
                        HeaderText="HostName"   
                        DataMemberBinding="{Binding Path=HostName, Mode=OneWay}" /> 
                    <telerik:GridViewDataColumn   
                        HeaderText="Type"   
                        DataMemberBinding="{Binding Path=Type, Mode=OneWay}" /> 
                    <telerik:GridViewDataColumn   
                        HeaderText="Domain" 
                        DataMemberBinding="{Binding Path=Domain, Mode=OneWay}" /> 
                </telerik:RadGridView.Columns> 
                <telerik:RadGridView.HierarchyChildTemplate> 
                    <DataTemplate> 
                        <telerik:RadGridView Name="Addresses" ShowGroupPanel="False" AutoGenerateColumns="False" ColumnsWidthMode="Auto"
                            <telerik:RadGridView.Columns> 
                                <telerik:GridViewDataColumn HeaderText="Address" DataMemberBinding="{Binding Path=Address, Mode=OneWay}"/> 
                            </telerik:RadGridView.Columns> 
                        </telerik:RadGridView> 
                    </DataTemplate> 
                </telerik:RadGridView.HierarchyChildTemplate> 
            </telerik:RadGridView> 
        </Grid> 
    </Window> 

  5. Kenneth
    Kenneth avatar
    70 posts
    Member since:
    Feb 2008

    Posted 16 Sep 2009 Link to this post

    Update --

    Was able to get this working - perfectly - by putting the child table in code behind. However, still no luck with putting it in XAML (my preferred method). 

    Read on the Siliverlight forum that there had been problems with HierarchyChildTemplate and AutoGenerateColumns. Apparently, even if AutoGenerateColumn is false, it would still attempt to create them, which would defeat the explicit XAML column definitions. Perhaps that is the problem here, since one of the columns requires OneWay binding.

    What do you think?

  6. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 21 Sep 2009 Link to this post

    Hello Kenneth,

    I could not reproduce the AutoGenerateColumns issue on my end. I have prepared a small sample project and attached it.

    Here is my XAML:

    <Window x:Class="TicketID_243249_HierarchyChildTemplate.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
        Title="Window1" Height="700" Width="600"
        <Grid> 
            <telerik:RadGridView  
                Name="clubsGrid"  
                Grid.Row="0" 
                AutoGenerateColumns="False" 
                ColumnsWidthMode="Auto"
                <telerik:RadGridView.Columns> 
                    <telerik:GridViewDataColumn  
                        Header="Name"  
                        DataMemberBinding="{Binding Name}"
                    </telerik:GridViewDataColumn> 
                    <telerik:GridViewDataColumn  
                        Header="Est."  
                        DataMemberBinding="{Binding Established}" 
                        DataFormatString="{}{0:yyyy}"
                    </telerik:GridViewDataColumn> 
                    <telerik:GridViewDataColumn  
                        Header="Stadium"  
                        DataMemberBinding="{Binding StadiumCapacity}" 
                        DataFormatString="{}{0:N0}"
                    </telerik:GridViewDataColumn> 
                </telerik:RadGridView.Columns> 
                <telerik:RadGridView.HierarchyChildTemplate> 
                    <DataTemplate> 
                        <telerik:RadGridView Name="playersGrid"  
                                             AutoGenerateColumns="False" 
                                             ColumnsWidthMode="Auto"
                            <telerik:RadGridView.Columns> 
                                <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name, Mode=OneWay}"
                                </telerik:GridViewDataColumn> 
                                <!-- 
                                <telerik:GridViewDataColumn Header="Number" DataMemberBinding="{Binding Number, Mode=OneWay}"
                                </telerik:GridViewDataColumn> 
                                <telerik:GridViewDataColumn Header="Position" DataMemberBinding="{Binding Position, Mode=OneWay}"
                                </telerik:GridViewDataColumn> 
                                <telerik:GridViewDataColumn Header="Country" DataMemberBinding="{Binding Country, Mode=OneWay}"
                                </telerik:GridViewDataColumn> 
                                --> 
                            </telerik:RadGridView.Columns> 
                        </telerik:RadGridView> 
                    </DataTemplate> 
                </telerik:RadGridView.HierarchyChildTemplate> 
            </telerik:RadGridView> 
        </Grid> 
    </Window> 
     

    And my code behind is simple:

    using System.Windows; 
    using Telerik.Windows.Controls; 
    using Telerik.Windows.Data; 
     
    namespace TicketID_243249_HierarchyChildTemplate 
        /// <summary> 
        /// Interaction logic for Window1.xaml 
        /// </summary> 
        public partial class Window1 : Window 
        { 
            public Window1() 
            { 
                InitializeComponent(); 
     
                this.clubsGrid.ItemsSource = Club.GetClubs(); 
                this.clubsGrid.TableDefinition.ChildTableDefinitions.Add( 
                    new GridViewTableDefinition 
                        { 
                            Relation = new PropertyRelation("Players"
                        }); 
            } 
        } 
     

    I do not get the child grid's columns auto-generated. Can you please take a look at it and see whether it works on you end?

    Best wishes,
    Ross
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
UI for WPF is Visual Studio 2017 Ready