mit
mit

Reputation: 119

WPF binding does not work for an ItemsControl

I am trying to bind a dependency property to another property but it does not seem to work. I have an ItemsControl within another ItemsControl as such:

<!--This is an itemscontrol in BigBox.xaml that contains bins-->
        <ItemsControl
            x:Name="ctrlBin"
            Grid.Column="1"
            Grid.Row="1"
            ItemsPanel="{StaticResource HorizontalStackPanel}"
            ItemsSource="{Binding Bins}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl 
                    ItemsSource="{Binding}"
                    ItemContainerStyle="{StaticResource BinViewContainer}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <local:BinView
                                x:Name="ctrlBin"
                                BorderBrush="Black"
                                BorderThickness="1"
                                Bin="{Binding}"
                                BinFlashStart="{Binding DashboardFlashStart}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        </ItemsControl>

This is in my 'BigBox'

        private bool? m_DashboardFlashStart;
        public bool? DashboardFlashStart
        {
            get=> m_DashboardFlashStart;
            set => Set(ref m_DashboardFlashStart, value);
        }

And this is in my BinView

        //This is in the BinView.xaml.cs 
    public static DependencyProperty BinFlashStartProperty =
        DependencyProperty.Register(
            "BinFlashStart",
            typeof(bool?),
            typeof(BinView),
            new PropertyMetadata(null, OnBinFlashStartSet));

    private static void OnBinFlashStartSet(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        BinVM Bin = ((BinView)sender).m_BinVM;
        Bin.CurBin.FlashBin = (bool?)e.NewValue;
    }

    public bool? BinFlashStart
    {
        get => (bool?) GetValue(BinFlashStartProperty);
        set => SetValue(BinFlashStartProperty, value);
    }

With that i get an error

System.Windows.Data Error: 40 : BindingExpression path error: 'DashboardFlashStart' property not found on 'object' ''Bin' (HashCode=-125066214)'. BindingExpression:Path=DashboardFlashStart; DataItem='Bin' (HashCode=-125066214); target element is 'BinView' (Name=''); target property is 'BinFlashStart' (type 'Nullable`1')

Why is it looking for DashboardFlashStart property on 'Bin'. I thought that is the source which comes from the BigBox. Like if i put a static value of "True" in BigBox.xaml for BinFlashStart instead of binding then that works. Why am i not able to bind to the DashboardFlashStart of BigBox.

Please if somebody can explain what is going on, that would be really helpful. I am new to this WPF.

Upvotes: 1

Views: 941

Answers (2)

mm8
mm8

Reputation: 169170

This should work provided that the DashboardFlashStart property is defined in the code-behind of the BigBox class, i.e. in BigBox.xaml.cs:

BinFlashStart="{Binding DashboardFlashStart, RelativeSource={RelativeSource AncestorType=local:BigBox}}"/>

Upvotes: 1

Jonathan Perennes
Jonathan Perennes

Reputation: 133

Your control is looking for a property named DashboardFlashStart in it's datacontext. But the property isn't in the "Bin" class but in the BigBox, so what you have to do is to specify for this binding the source of the binding to the bigbox datacontext.

Change your Binding BinFlashStart="{Binding DashboardFlashStart}"

With something like this

{Binding DataContext.DashboardFlashStart, ElementName=ctrlBin}"

Upvotes: 1

Related Questions