Note that there are some explanatory texts on larger screens.

plurals
  1. POsilverlight binding combobox in nested controls
    text
    copied!<p>I have 2 user controls one named Filters and one named FilterItem</p> <p>Filter looks like this:</p> <pre><code>&lt;UserControl xmlns:my="clr-namespace:AttorneyDashboard.Views.UserControls" x:Class="AttorneyDashboard.Views.UserControls.Filters" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:helpers="clr-namespace:AttorneyDashboard.Helpers" mc:Ignorable="d" d:DesignHeight="150" d:DesignWidth="590" x:Name="FiltersRoot"&gt; &lt;Grid&gt; &lt;ListBox x:Name="myListBox" ItemsSource="{Binding Path=FilterItems, ElementName=FiltersRoot}" &gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;my:FilterItem ColumnsList="{Binding Path=Columns_, ElementName=FiltersRoot}" /&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Code Behind:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Data; using System.Collections.ObjectModel; using System.Diagnostics; using AttorneyDashboard.Helpers; namespace AttorneyDashboard.Views.UserControls { public class MyItems { public string Header { get; set; } } public partial class Filters : UserControl { public Filters() { InitializeComponent(); } private DependencyProperty FilterItemsProperty = DependencyProperty.Register("FilterItems", typeof(ObservableCollection&lt;FilterDescriptor&gt;), typeof(Filters), new PropertyMetadata(null, new PropertyChangedCallback(OnChangeFilterItems))); public ObservableCollection&lt;FilterDescriptor&gt; FilterItems { get { return (ObservableCollection&lt;FilterDescriptor&gt;)GetValue(FilterItemsProperty); } set { SetValue(FilterItemsProperty, value); } } public static void OnChangeFilterItems(DependencyObject d, DependencyPropertyChangedEventArgs e) { } public List&lt;MyItems&gt; Columns_ { get { List&lt;MyItems&gt; list = new List&lt;MyItems&gt;(); list.Add(new MyItems() { Header = "test1" }); list.Add(new MyItems() { Header = "test2" }); list.Add(new MyItems() { Header = "test3" }); return list; } } } } </code></pre> <p>FilterItems looks like this</p> <pre><code>&lt;UserControl x:Class="AttorneyDashboard.Views.UserControls.FilterItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="23" d:DesignWidth="590" xmlns:my="clr-namespace:AttorneyDashboard.Helpers" x:Name="FilterItemRoot"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;ComboBox Height="23" HorizontalAlignment="Left" Name="FieldName" VerticalAlignment="Top" Width="120" Margin="5,0,0,0" ItemsSource="{Binding Path=ColumnsList, ElementName=FilterItemRoot}" SelectedItem="{Binding PropertyPath, Mode=TwoWay}" DisplayMemberPath="Header"/&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>Code behind:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.ObjectModel; using AttorneyDashboard.Helpers; using System.Windows.Data; namespace AttorneyDashboard.Views.UserControls { public partial class FilterItem : UserControl { public FilterItem() { InitializeComponent(); } private DependencyProperty ColumnsListProperty = DependencyProperty.Register("ColumnsList", typeof(List&lt;MyItems&gt;), typeof(FilterItem), new PropertyMetadata(null, new PropertyChangedCallback(OnChangeColumns))); public List&lt;MyItems&gt; ColumnsList { get { return (List&lt;MyItems&gt;)GetValue(ColumnsListProperty); } set { SetValue(ColumnsListProperty, value); } } public static void OnChangeColumns(DependencyObject d, DependencyPropertyChangedEventArgs e) { } } } </code></pre> <p>The number of FilterItems is ok (this means that FilterItems binding works ok), but only the Combobox of the last FilterItem is populated... And I do not know what exactly is wrong...</p> <p><strong>Update:</strong> I found why but I stll do not know a solution... It seams that the content of FilterItem is binded before his properties are.. So the combobox in FilterItem is binded before the Columns property is binded...</p>
 

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