c# - WPF DataGrids for hierarchical information -


i have application contains observablecollection<foo>, , foo in turn contains observablecollection<bar>. pair of datagrids, 1 showing collection of foo objects in application , other showing collection of bar objects in foo that's selected in first datagrid (and want able add, update , delete entries in both datagrids).

so far i've got following xaml:

<window x:class="test.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="350" width="525">     <grid>         <grid.columndefinitions>             <columndefinition width="*"/>             <columndefinition width="auto"/>             <columndefinition width="*"/>         </grid.columndefinitions>         <datagrid grid.column="0" itemssource="{binding foos}" autogeneratecolumns="false">             <datagrid.columns>                 <datagridtextcolumn header="foo name" binding="{binding name}" width="auto" isreadonly="false" />             </datagrid.columns>         </datagrid>         <gridsplitter horizontalalignment="right" verticalalignment="stretch" grid.column="1" resizebehavior="previousandnext" width="5" background="gray" />         <datagrid grid.column="2">             <datagrid.columns>                 <datagridtextcolumn header="bar name" width="auto" isreadonly="false"/>             </datagrid.columns>         </datagrid>     </grid> </window> 

and following code:

using system; using system.windows; using system.collections.objectmodel;  namespace test {     public class foo     {         static int _nextid;         public int id { get; private set; }         public string name { get; set; }         public observablecollection<bar> bars { get; set; }         public foo()         {             id = _nextid++;             name = string.empty;             bars = new observablecollection<bar>();         }     }      public class bar     {         static int _nextid;         public int id { get; private set; }         public string name { get; set; }         public bar()         {             id = _nextid++;             name = string.empty;         }     }     /// <summary>     /// interaction logic mainwindow.xaml     /// </summary>     public partial class mainwindow : window     {         public observablecollection<foo> foos { get; set; }         public mainwindow()         {             foos = new observablecollection<foo>();             foo newfoo;             (int index = 0; index < 5; ++index)             {                 newfoo = new foo();                 newfoo.name = string.format("foo {0}", index);                 foos.add(newfoo);             }             initializecomponent();             datacontext = this;         }     } } 

obviously i've not bound 2nd datagrid yet, because i've not got faintest idea how it! examples can find assume i'm binding datatables, not custom objects, , bind relation on datatables. don't understand binding yet. can tell me how bind 2nd table?

(and yes, if you've seen other recent questions, giving wpf shot after not getting on in days).

thanks in advance.

hi if want editable grids first of have implement inotifypropertychanged like

    public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();         datacontext = new viewmodel();     } }  public class viewmodel  {     public viewmodel()     {         foos = new observablecollection<foo>();     }      public observablecollection<foo> foos { get; set; } }  public class foo : inotifypropertychanged {     static int _nextid;     public int id { get; private set; }     public observablecollection<bar> bars { get; set; }     public foo()     {         id = _nextid++;         name = string.empty;         bars = new observablecollection<bar>();     }     private string name;      public string name     {                 {             return name;         }         set         {             name = value;             notify("name");         }     }      private void notify(string propname)     {         if (propertychanged != null)             propertychanged(this, new propertychangedeventargs(propname));     }      public event propertychangedeventhandler propertychanged;  }  public class bar : inotifypropertychanged {     static int _nextid;     public int id { get; private set; }     public bar()     {         id = _nextid++;         name = string.empty;     }      private string name;      public string name     {                 {             return name;         }         set         {             name = value;             notify("name");         }     }      private void notify(string propname)     {         if (propertychanged != null)             propertychanged(this, new propertychangedeventargs(propname));     }      public event propertychangedeventhandler propertychanged; } 

in xaml binding first grid corrrect , second grid can set itemssource the selecteditem of first grid using elementname

<datagrid grid.column="2" itemssource="{binding elementname=gridtop, path=selecteditem.bars}">         <datagrid.columns>             <datagridtextcolumn header="bar name" binding="{binding name}" width="auto" isreadonly="false"/>         </datagrid.columns>     </datagrid> 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -