简体   繁体   English

WPF绑定不适用于ItemsControl

[英]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: 我在另一个ItemsControl中有一个ItemsControl,例如:

<!--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' 这是在我的“ BigBox”中

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

And this is in my BinView 这是在我的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'. 为什么要在“ Bin”上查找DashboardFlashStart属性。 I thought that is the source which comes from the BigBox. 我认为这是来自BigBox的来源。 Like if i put a static value of "True" in BigBox.xaml for BinFlashStart instead of binding then that works. 就像如果我在BigBox.xaml中将BinFlashStart的静态值“ True”放到而不是绑定中那样,那行得通。 Why am i not able to bind to the DashboardFlashStart of BigBox. 为什么我无法绑定到BigBox的DashboardFlashStart。

Please if somebody can explain what is going on, that would be really helpful. 如果有人可以解释发生了什么,那将很有帮助。 I am new to this WPF. 我是这个WPF的新手。

Your control is looking for a property named DashboardFlashStart in it's datacontext. 您的控件正在其数据上下文中查找名为DashboardFlashStart的属性。 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. 但是该属性不是在“ Bin”类中,而是在BigBox中,因此您需要为此绑定指定与bigbox数据上下文的绑定源。

Change your Binding BinFlashStart="{Binding DashboardFlashStart}" 更改您的绑定BinFlashStart="{Binding DashboardFlashStart}"

With something like this 像这样

{Binding DataContext.DashboardFlashStart, ElementName=ctrlBin}"

只要在BigBox类的代码隐藏中(即在BigBox.xaml.cs )定义了DashboardFlashStart属性,这应该可以工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM