如何将集合绑定到WPF中的自定义控件 [英] how to bind collection to custom control in wpf

查看:78
本文介绍了如何将集合绑定到WPF中的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个自定义控件,我想将一个集合传递给它,以便控件显示该集合,我的代码如下:

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>

并在自定义控件日历中

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);
    }
}

我在generic.xaml中使用

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?

推荐答案

我的问题现在已经解决了,这要归功于@Luke Woodward,而我刚刚有了另一个我在usercontrol中使用自定义控件,并且usercontrol是ListItem
中的一个问题,我修改了绑定表达式

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}" >

,而customcontrol是

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>()));

,并在Generic.xaml中修改为@HighCore发布

and in the Generic.xaml modified as @HighCore posted

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

终于工作了。
感谢@Luke Woodward和@HighCore

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

这篇关于如何将集合绑定到WPF中的自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆