1

I am building a custom control and I want to pass a collection to it so that control display that collection, my code is as the following :

<gm:Calendar SubscriptionSource="{Binding Subscriptions}"></gm:Calendar>

and in Custom control "Calendar"

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

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

I use in generic.xaml

<ItemsControl ItemsSource="{Binding SubscriptionSource}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!--Box-->
            <Border BorderBrush="Black" BorderThickness="1" Padding="0">
                <Border Name="InnerBorder" BorderBrush="{Binding Path=Day, Converter={StaticResource DayBorderColorConverter}}" BorderThickness="2">
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Style.Triggers>
                                <!--Current Day-->
                                <DataTrigger Binding="{Binding IsToday}" Value="true">
                                    <Setter Property="Border.Background">
                                        <Setter.Value>
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                <GradientStop Color="#FF1EA6C8" Offset="0"/>
                                                <GradientStop Color="#FF0691B3" Offset="1"/>
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <DockPanel>
                        <!--Day Number-->
                        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" FlowDirection="RightToLeft">
                            <TextBlock TextAlignment="Right" Text="{Binding Day.Date, Converter={StaticResource DateConverter}, ConverterParameter=DAY}" FontSize="12" Margin="5,5,5,5" >
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsTargetMonth}" Value="false">
                                                <Setter Property="TextBlock.Foreground" Value="Gray"></Setter>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </StackPanel>
                        <CheckBox IsEnabled="{Binding IsEnabled}" Style="{StaticResource DiscreteCheckBoxStyle}" />
                    </DockPanel>
                </Border>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="6" Columns="7" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

I want to Bind Subscriptions observable collection to the calendar custom control so I can use the collection in the custom control, is there is away to do this?

Robert
  • 2,407
  • 1
  • 24
  • 35
Muhammad Alaa
  • 608
  • 1
  • 10
  • 15
  • The value of the dependency property will be empty during the constructor because at that time WPF hasn't even constructed the control, let alone assigned values to dependency properties. You would be better off using a PropertyChangedCallback on the dependency property instead of trying to access the list in the constructor. – Luke Woodward Jan 19 '13 at 14:10
  • thank you for your response, as you can see I want to Bind Subscriptions observable collection to the calendar custom control so I can use the collection in the custom control, is there is away to do this? – Muhammad Alaa Jan 19 '13 at 14:19
  • Like I said, use a PropertyChangedCallback and you will be able to get access to the collection in the custom control. First Google search result: http://stackoverflow.com/questions/5498517/how-to-use-propertychangedcallback. – Luke Woodward Jan 19 '13 at 14:26
  • I edited the question may be it was not clear what I am looking for. – Muhammad Alaa Jan 19 '13 at 14:33
  • I edit again and post the part of generic.xaml in the custom control calendar – Muhammad Alaa Jan 19 '13 at 14:57
  • I also tried to follow the http://stackoverflow.com/questions/5664091/wpf-custom-control-dependencyproperty-of-collection-type but did not work for me – Muhammad Alaa Jan 19 '13 at 14:59
  • yes it is in a ControlTemplate in a style. – Muhammad Alaa Jan 19 '13 at 15:02
  • You've changed your question, so my earlier comments no longer apply. Still, I created a custom control in a new WPF application and added the DP above. I copied your XAML into my Themes\Generic.xaml, removed from it references to styles and converters I don't have and changed the binding as suggested by @HighCore. After doing that, the custom control was working, in that I was able to see a list of items in the calendar. – Luke Woodward Jan 20 '13 at 13:05
  • @Luke Woodward Would you please send me that solution you created and worked for you, my email is eng.muhammad_alaa@yahoo.com , may be I can find what I am doing wrong. – Muhammad Alaa Jan 23 '13 at 07:07
  • I'm not going to email you the solution I created. Please don't ask people to do this. However, I have uploaded this solution to http://www.lack-of.org.uk/stuff/CustomControlChaos2.zip – Luke Woodward Jan 25 '13 at 21:00
  • @Luke Woodward I do not understand why is this wrong to email me the solution you created ? but many thanks for helping me – Muhammad Alaa Jan 26 '13 at 09:43
  • 1
    There's two reasons why I didn't do that. Firstly, if I were to send you an email, you'd then have my personal email address. Secondly, the intention of Stack Overflow is that questions are to help future visitors as well as you. Having part of the problem hidden away in a private email conversation makes it harder for future visitors to benefit from this question. – Luke Woodward Jan 26 '13 at 13:32
  • @Luke Woodward Thanks a lot, I see why it is important for your privacy but when I asked for solution just to compare with my code to see what I am doing wrong. Now I had another problem this code in usercontrol that is bounded to a list each item contains ObservableCollection so when using that code I get empty collection in the custom control – Muhammad Alaa Jan 27 '13 at 09:28
  • If I understand you correctly, you actually have a collection of collections. If this is so, you have not made this at all clear in your question. **Edit your question to include ALL relevant details.** – Luke Woodward Jan 27 '13 at 11:03

2 Answers2

0

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

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • I tried {TemplateBinding SubscriptionSource} but still get the collection empty. – Muhammad Alaa Jan 19 '13 at 15:06
  • Are you sure the actual collection is getting correctly bound to the control? I mean, check the VS output window to see if there are any binding errors. Otherwise, use Snoop to inspect the running application and check the state of the control and its visual children. – Federico Berasategui Jan 19 '13 at 15:31
  • I tried to bind the subscriptions List to ListBox as a test and it is working but what I need is to bind the list to the custom control – Muhammad Alaa Jan 19 '13 at 21:20
  • nop! must use templatebinding in template control style! – zamoldar Jul 01 '16 at 15:44
0

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

Muhammad Alaa
  • 608
  • 1
  • 10
  • 15