绑定到 UserControl 的 DependencyProperty
我正在尝试从 http:// 调整 WPF 工具包日历的解决方案msdn.microsoft.com/en-us/magazine/dd882520.aspx 但我在用户控件上的绑定正常工作时遇到问题。我尝试过使用 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);
}
}
我有另一个控件,发生同样的事情,所以我认为我可能遗漏了一些东西。
I'm trying to adapt a soltuion for the WPF toolkit's calendar from http://msdn.microsoft.com/en-us/magazine/dd882520.aspx but I'm having problems getting a binding on the usercontrol to work. I've tried using FindAncestor and ElementName, but I just get a binding error.
I think it might have something to do with the tooltip and it's DataContext in the calendar. Has anyone else had this problem?
<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>
The error is:
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')
And the declaration for the 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);
}
}
There's another control I have where the same thing happens, so I think I may be missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ConversionCollection 是“MyControl”用户控件上的属性吗?如果是这样,您需要将此用户控件的 DataContext 设置为其自身,因为此属性的默认值为 null。
编辑:我注意到在 ConversionCollectionProperty 依赖属性的声明中,您将名称声明为“Meals”,但该属性实际上是 ConversionCollection。这些名称需要匹配才能使依赖属性按预期运行。
Is ConversionCollection a property on the 'MyControl' user control? If so, you need to set the DataContext for this user control to itself as the default value for this property is null.
Edit: I noticed in your declaration of the ConversionCollectionProperty dependency property you declared the name as "Meals" but the property is actually ConversionCollection. These names need to match in order for the dependency property to operate as expected.