how to bind collection to custom control in wpf

风格不统一 提交于 2019-12-02 04:19:32

If the ItemsControl is inside the ControlTemplate, then Change the {Binding SubscriptionSource} for {TemplateBinding SubscriptionSource}

My problem is now Solved thanks to @Luke Woodward and I just had another problem that I use the custom control inside usercontrol and that usercontrol was an item inside ListItem I modified the binding expression

<gm:Calendar SubscriptionSource="{Binding Path=Subscriptions,Mode=TwoWay}" >

and the customcontrol is

static Calendar()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Calendar), new FrameworkPropertyMetadata(typeof(Calendar)));
    }

    public ObservableCollection<SubscriptionDay> SubscriptionSource
    {
        get { return (ObservableCollection<SubscriptionDay>)GetValue(SubscriptionSourceProperty); }
        set { SetValue(SubscriptionSourceProperty, value); }
    }

    public static readonly DependencyProperty SubscriptionSourceProperty =
        DependencyProperty.Register("SubscriptionSource", typeof(ObservableCollection<SubscriptionDay>), typeof(Calendar), new FrameworkPropertyMetadata(new ObservableCollection<SubscriptionDay>()));

and in the Generic.xaml modified as @HighCore posted

<ItemsControl ItemsSource="{TemplateBinding SubscriptionSource}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>......

and finally worked. Thanks to @Luke Woodward and @HighCore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!