简体   繁体   English

XAML不一致的绑定

[英]XAML inconsistent binding

I have a custom UserControl called ReferencedItem . 我有一个称为ReferencedItem的自定义UserControl。 It should take a Guid called ItemId. 它应该使用一个名为ItemId的Guid。 It is implemented as such: 它是这样实现的:

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

The ItemsSource will be made up of Reference objects defined as: ItemsSource将由定义为以下内容的Reference对象组成:

public class Reference
{
    public Guid Id { get; set; }
}

Now when binding this ReferencedItem the value is not set as intended. 现在,在绑定此ReferencedItem该值未按预期设置。 Here is the code I want to work, but does not bind as intended: 这是我要工作的代码,但未按预期绑定:

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

I have tried: 我努力了:

 <local:ReferencedItem ItemId="128d48f0-f061-49fb-af49-b8e4ef891d03" Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>

This works as expected, the OnItemIdChanged method is triggered. 如预期的那样,将触发OnItemIdChanged方法。

<Label Content="{Binding Id}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="90"/>

This works as expected, a label is rendered with the Id. 如预期的那样,将使用ID呈现标签。

Is there something I'm missing here? 我在这里想念什么吗? From what I can tell the data is available at bind time -- it just doesn't bind under the exact conditions I need it to :) 据我所知,数据在绑定时可用-在我需要的确切条件下它只是不绑定:)

Thanks for any input! 感谢您的输入!

EDIT: 编辑:

Here is the code-behind for ReferencedItemList , the first block of XAML posted above: 这是上面发布的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;
    }
}

The commented line has been experimented with in place of the ItemsSource="{Binding References}" that was defined in the XAML. 已对注释行进行了实验,以代替XAML中定义的ItemsSource="{Binding References}"

I don't think I can successfully post the code for ReferencedItemListCtrlViewModel without going down a rabbit hole -- needless to say it has a property References of type ObservableCollection<Reference> where Reference is defined earlier in this post. 我认为我不能成功地发布ReferencedItemListCtrlViewModel的代码而不会陷入麻烦–不用说它具有类型为ObservableCollection<Reference>的属性References ,其中Reference是在本文前面定义的。

ReferencedItem.xaml: 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>

I just wanted to post the answer (explanation) I came across. 我只是想发布我遇到的答案(解释)。

The problem was changing the DataContext of my ReferencedItem user control in the constructor. 问题是在构造函数中更改了我ReferencedItem用户控件的DataContext。 The view would instantiate a ReferencedItem and alter the DataContext - so once it was time to bind I had already flipped the context from the intended Reference . 该视图将实例化ReferencedItem并更改DataContext-因此,一旦需要绑定,我就已经从预期的Reference翻转了上下文。

There are multiple ways to resolve the timing - all project dependent. 解决计时的方法有多种-取决于项目。 Either avoid setting the DataContext all together, set it post binding, or change the context on other items as appropriate. 避免一起设置DataContext,在绑定后对其进行设置,或者适当地更改其他项目的上下文。

Much thanks to Sinatr , Andrew Stephens , and Mike Strobel for all mentioning this at one point or another -- just took me some time to actually reach it. 非常感谢SinatrAndrew StephensMike Strobel在一到另一点都提到了这一点-我花了一些时间才真正达到目标。 I don't think there's a way to assign credit to a comment, but let me know if there is. 我认为没有办法为评论添加功劳,但请告诉我是否存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM