1

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

<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 中使用

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

我想将 Subscriptions 可观察集合绑定到日历自定义控件,以便可以在自定义控件中使用该集合,是否可以这样做?

4

2 回答 2

0

如果在ItemsControl里面ControlTemplate,那么改变{Binding SubscriptionSource}for{TemplateBinding SubscriptionSource}

于 2013-01-19T15:03:32.683 回答
0

感谢@Luke Woodward,我的问题现在得到了解决,我刚刚遇到了另一个问题,我在 usercontrol 中使用了自定义控件,而 usercontrol 是 ListItem 中的一个项目,我修改了绑定表达式

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

并且自定义控件是

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

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

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

终于奏效了。感谢@Luke Woodward 和@HighCore

于 2013-01-27T11:44:32.290 回答