Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency Property inner Binding in custom control
    primarykey
    data
    text
    <p>I'm trying to bind the ItemsSource property of an ItemsControl contained in my UserControl (a group of radiobuttons) to a property of the same UserControl. This should be driven by the DependencyProperty when the UserControl is used. But I am not able to make it work, if instead I hardcode the control name the assignment works correctly.</p> <p>This is the class : </p> <pre><code>public partial class GroupListBox : UserControl { public GroupListBox() { InitializeComponent(); } private List&lt;RadioButton&gt; _itemsList = new List&lt;RadioButton&gt;(); public List&lt;RadioButton&gt; ItemsList { get { return _itemsList; } set { _itemsList = value; } } public IEnumerable ItemsSource { get { return (IEnumerable)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(GroupListBox), new FrameworkPropertyMetadata(null, OnItemsSourceChanged)); private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = (GroupListBox)d; var grpName = control.GetHashCode().ToString(); // used to generate a unique GroupName var oldValue = (IEnumerable)e.OldValue; var newValue = (IEnumerable)e.NewValue; if ((e.NewValue == null) &amp;&amp; !BindingOperations.IsDataBound(d, ItemsSourceProperty)) { control.ItemsList.Clear(); } else { var radioItems = (from object item in newValue select new RadioButton() { Content = item, GroupName = grpName }).ToList(); //control.container.ItemsSource = radioItems; // *** this works control.ItemsList = radioItems; // *** this doesn`t } } </code></pre> <p>this is the XAML</p> <pre><code>&lt;Border x:Name="brdMain"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;ColumnDefinition Width="*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock x:Name="txtTitle" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Text="" /&gt; &lt;Image x:Name="imgImage" Grid.Column="0" Grid.Row="1" /&gt; &lt;ScrollViewer x:Name="scroll" Grid.Row="1" Grid.Column="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" CanContentScroll="True"&gt; &lt;!--This is the control I want to bind--&gt; &lt;ItemsControl x:Name="container" ItemsSource="{Binding Path=ItemsList}" &gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;/ItemsControl&gt; &lt;/ScrollViewer&gt; &lt;/Grid&gt; &lt;/Border&gt; </code></pre> <p>used like this:</p> <pre><code>&lt;cust:GroupListBox ItemsSource="{Binding SymmetryAxis}"&gt;&lt;/cust:GroupListBox&gt; </code></pre> <p>I'm sure there is something wrong with my control binding but I don`t know what. Any clue?</p> <p>EDIT : I did some little change in the XAML : now it looks like this :</p> <pre><code>&lt;ItemsControl Name="container" Width="{Binding ElementName=scroll, Path=ViewportWidth}" ItemsSource="{Binding Path=ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GroupListBox}}}" ItemTemplate="{StaticResource ItemPanelDatatemplate}" /&gt; </code></pre> <p>and this is the DataTemplate :</p> <pre><code>&lt;DataTemplate DataType="{x:Type ItemsPanelTemplate}" x:Key="ItemPanelDatatemplate"&gt; &lt;RadioButton Content="{Binding}" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}, Path=Name}"&gt;&lt;/RadioButton&gt; &lt;/DataTemplate&gt; </code></pre> <p>This anyway gives problems with the GroupName because it`s always empty and then all the RadioButtons act like if they are in a single group.</p> <p>How can I obtain the same result like in the code behind (for example assigning a unique name for each group?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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