Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: What's the proper way to do nested Binding
    primarykey
    data
    text
    <p>I have 3 models: Machine, Part, and Component. On my ui, I have bound one listbox to a list of Machines and a second listbox to the selected Machine's list of Parts. Now when the user selects a part, I would like to bind a datagrid to the selected part's list of components. I tried setting the datagrid's itemssource to: </p> <p><code>ItemsSource="{Binding ElementName=PartSelectList, Path=SelectedItem.Components}"</code></p> <p>I also tried setting its datacontext and itemssource</p> <p><code>DataContext={Binding ElementName=PartSelectList, Path=SelectedItem}</code></p> <p><code>ItemsSource={Binding Components}</code></p> <p>However the datagrid does not automatically update if changes are made to the part's list of components via methods such as</p> <p><code>part.Bags.Add(new Component(..));</code> </p> <p>Anyone have suggestions on how I should approach this?</p> <p>Update: I have a class MachineCollection which implements INotifyPropertyChanged. In my xaml.cs code I create my MachineCollection object and bind the MainGrid to it's Machines property:</p> <p>Page1.xaml.cs:</p> <pre><code>private MachineCollection machines public Page1() { machineCollection = new MachineCollection(); MainGrid.DataContext = machineCollection.Machines actionThread = new Thread(InitLists); actionThread.Start(); } public void InitLists() { machineCollection.Machines.AddRange(dbCon.GetAllMachines()); } </code></pre> <p>Classes:</p> <pre><code>public class MachineCollection : INotifyPropertyChanged { public List&lt;Machine&gt; Machines { get { return machines; } set { machines = value; NotifyPropertyChanged("Machines"); } } } public class Machine : INotifyPropertyChanged { public List&lt;Part&gt; Parts { get { return parts; } set { parts = value; NotifyPropertyChanged("Parts"); } } public class Part : INotifyPropertyChanged { public List&lt;Component&gt; Components { get { return components; } set { components = value; NotifyPropertyChanged("Components"); } } public class Component : INotifyPropertyChanged { public int Length { get { return length; } set { length = value; NotifyPropertyChanged("Length"); } } </code></pre> <p>XAML:</p> <pre><code>&lt;Page&gt; &lt;Grid Name="MainGrid"&gt; &lt;ListBox x:Name="MachineSelectList" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="114" Width="231" Foreground="White" Background="#FF7A7A7A" FontSize="16" BorderBrush="#FFC13131" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid Width="200"&gt; &lt;TextBlock Text="{Binding SerialNumber}" TextAlignment="Left" HorizontalAlignment="Left" VerticalAlignment="Center"/&gt; &lt;Button Name="DeleteMachine" Tag="{Binding SerialNumber}" HorizontalAlignment="Right" VerticalAlignment="Center" Height="20" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" Focusable="False" Click="Btn_Click" &gt; &lt;Image Source="..\Resources\Images\delete.png" Stretch="Uniform"/&gt; &lt;/Button&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;ListBox Name="PartSelectList" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="164" Width="231" Background="#FF7A7A7A" Foreground="White" FontSize="16" BorderBrush="#FFC13131" ItemsSource="{Binding Path=Parts}" IsSynchronizedWithCurrentItem="True"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid Width="200"&gt; &lt;TextBlock Text="{Binding PartNumber}" TextAlignment="Left" HorizontalAlignment="Left" VerticalAlignment="Center"/&gt; &lt;Button Name="DeletePart" Tag="{Binding PartNumber}" HorizontalAlignment="Right" VerticalAlignment="Center" Height="20" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" Focusable="False" Click="Btn_Click" &gt; &lt;Image Source="..\Resources\Images\delete.png" Stretch="Uniform"/&gt; &lt;/Button&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; &lt;/Page&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload