[英]XAML inconsistent binding
我有一个称为ReferencedItem
的自定义UserControl。 它应该使用一个名为ItemId的Guid。 它是这样实现的:
private static void OnItemIdChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs dpArgs)
{
//Do something
}
public static readonly DependencyProperty ItemIdProperty = DependencyProperty.Register("ItemId", typeof(Guid?), typeof(ReferencedItem), new FrameworkPropertyMetadata(
// use an empty Guid as default value
Guid.Empty,
// tell the binding system that this property affects how the control gets rendered
FrameworkPropertyMetadataOptions.AffectsRender,
// run this callback when the property changes
OnItemIdChanged
));
public Guid? ItemId
{
get { return (Guid?)GetValue(ItemIdProperty); }
set { SetValue(ItemIdProperty, value); }
}
public ReferencedItem()
{
InitializeComponent();
ViewModel = new ReferencedItemCtrlViewModel();
DataContext = ViewModel;
}
ItemsSource
将由定义为以下内容的Reference
对象组成:
public class Reference
{
public Guid Id { get; set; }
}
现在,在绑定此ReferencedItem
该值未按预期设置。 这是我要工作的代码,但未按预期绑定:
<ItemsControl x:Name="ReferenceStack" ItemsSource="{Binding References}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ReferencedItem ItemId="{Binding Id}" Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我努力了:
<local:ReferencedItem ItemId="128d48f0-f061-49fb-af49-b8e4ef891d03" Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
如预期的那样,将触发OnItemIdChanged
方法。
<Label Content="{Binding Id}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="90"/>
如预期的那样,将使用ID呈现标签。
我在这里想念什么吗? 据我所知,数据在绑定时可用-在我需要的确切条件下它只是不绑定:)
感谢您的输入!
编辑:
这是上面发布的XAML的第一块ReferencedItemList
的代码背后:
public partial class ReferencedItemList : UserControl
{
protected ReferencedItemListCtrlViewModel ViewModel;
public ReferencedItemList()
{
InitializeComponent();
ViewModel = new ReferencedItemListCtrlViewModel();
DataContext = ViewModel;
}
public void Load(Guid id, string name)
{
ViewModel.Load(id, name);
//ReferenceStack.ItemsSource = ViewModel.References;
}
}
已对注释行进行了实验,以代替XAML中定义的ItemsSource="{Binding References}"
。
我认为我不能成功地发布ReferencedItemListCtrlViewModel
的代码而不会陷入麻烦–不用说它具有类型为ObservableCollection<Reference>
的属性References
,其中Reference
是在本文前面定义的。
ReferencedItem.xaml:
<v:BaseUserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</v:BaseUserControl.Resources>
<StackPanel Orientation="Horizontal">
<Image x:Name="LinkIcon" Visibility="{Binding HasReference, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTip="View Referenced Item" Source="/Images/link.png" Height="18" MouseUp="LinkIcon_MouseUp"/>
<TextBlock x:Name="ReferencedObjectDesc" Text="{Binding ReferenceHierarchy}" FontStyle="Italic" VerticalAlignment="Center" />
</StackPanel>
我只是想发布我遇到的答案(解释)。
问题是在构造函数中更改了我ReferencedItem
用户控件的DataContext。 该视图将实例化ReferencedItem
并更改DataContext-因此,一旦需要绑定,我就已经从预期的Reference
翻转了上下文。
解决计时的方法有多种-取决于项目。 避免一起设置DataContext,在绑定后对其进行设置,或者适当地更改其他项目的上下文。
非常感谢Sinatr , Andrew Stephens和Mike Strobel在一到另一点都提到了这一点-我花了一些时间才真正达到目标。 我认为没有办法为评论添加功劳,但请告诉我是否存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.