绑定到UserControl的DependencyProperty

时间:2009-10-20 10:34:38

标签: wpf binding wpftoolkit dependency-properties

我正在尝试从http://msdn.microsoft.com/en-us/magazine/dd882520.aspx调整WPF工具包日历的解决方案但我在使用usercontrol上的绑定时遇到问题。我尝试过使用FindAncestor和ElementName,但我只是遇到了绑定错误。

我认为它可能与工具提示和日历中的DataContext有关。还有其他人有这个问题吗?

<UserControl x:Class="ChickenPing.MealCalendar"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:conv="clr-namespace:ChickenPing.Converters"
    xmlns:wpf="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"
    xmlns:vsm="clr-namespace:System.Windows;assembly=WPFToolkit"
    xmlns:loc="clr-namespace:ChickenPing"
    x:Name="root">
    <wpf:Calendar x:Name="calendar">
        <wpf:Calendar.Resources>
            <conv:IconConverter x:Key="IconConverter"/>
            <conv:MealCalendarConverter x:Key="MealCalendarConverter" />
            <!--LinearGradientBrush x:Key="MealBackgroundFill" StartPoint="0,0"  EndPoint="0,1">
                <GradientStop Color=""
            </LinearGradientBrush-->
        </wpf:Calendar.Resources>
        <wpf:Calendar.CalendarDayButtonStyle>
            <Style TargetType="primitives:CalendarDayButton">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="primitives:CalendarDayButton">
                            <Grid>
                                <!Grid.ToolTip>
                                    <ToolTip>
                                        <ToolTip.DataContext>
                                            <MultiBinding Converter="{StaticResource MealCalendarConverter}">
                                                <Binding Path="PlacementTarget.DataContext" RelativeSource="{x:Static RelativeSource.Self}"/>
                                                <Binding Path="Meals">
                                                    <Binding.RelativeSource>
                                                        <RelativeSource Mode="FindAncestor" AncestorType="{x:Type loc:MealCalendar}" />
                                                    </Binding.RelativeSource>
                                                </Binding>
                                            </MultiBinding>
                                        </ToolTip.DataContext>

错误是:

System.Windows.Data Warning: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MyAssembly.MyControl', AncestorLevel='1''. BindingExpression:Path=ConversionCollection; DataItem=null; target element is 'ToolTip' (Name=''); target property is 'DataContext' (type 'Object')

DependencyProperty的声明:

public static readonly DependencyProperty MealsProperty = DependencyProperty.Register("Meals", typeof(Dictionary<DateTime, IEnumerable<PlannedMealGroup>>), typeof(MealCalendar), new UIPropertyMetadata(new Dictionary<DateTime, IEnumerable<PlannedMealGroup>>()));
public Dictionary<DateTime, IEnumerable<PlannedMealGroup>> Meals {
    get { return base.GetValue(MealsProperty) as Dictionary<DateTime, IEnumerable<PlannedMealGroup>>; }
    set { 
        base.SetValue(MealsProperty, value);
    }
}

我有另一个控件,我发生同样的事情,所以我想我可能会遗漏一些东西。

1 个答案:

答案 0 :(得分:0)

ConversionCollection是'MyControl'用户控件的属性吗?如果是这样,您需要将此用户控件的DataContext设置为自身,因为此属性的默认值为null。

public MyControl()
{
    DataContext = this;
}

编辑:我注意到在您声明的ConversionCollectionProperty依赖项属性中,您将名称声明为“Meals”,但该属性实际上是ConversionCollection。这些名称需要匹配才能使依赖项属性按预期运行。

public static readonly DependencyProperty ConversionCollectionProperty =
    DependencyProperty.Register(
        "ConversionCollection", 
        typeof(Dictionary<DateTime, IEnumerable<PlannedMealGroup>>), 
        typeof(MyControl), 
        new UIPropertyMetadata(new Dictionary<DateTime,
        IEnumerable<PlannedMealGroup>>()));

public Dictionary<DateTime, IEnumerable<PlannedMealGroup>> ConversionCollection 
{
    get 
    { 
        return base.GetValue(ConversionCollectionProperty) as Dictionary<DateTime, IEnumerable<PlannedMealGroup>>; 
    }
    set 
    { 
        base.SetValue(ConversionCollectionProperty, value);
    }