how to bind collection to custom control in wpf

倾然丶 夕夏残阳落幕 提交于 2019-12-02 12:39:54

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/14415097/how-to-bind-collection-to-custom-control-in-wpf

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