簡體   English   中英

XAML不一致的綁定

[英]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,在綁定后對其進行設置,或者適當地更改其他項目的上下文。

非常感謝SinatrAndrew StephensMike Strobel在一到另一點都提到了這一點-我花了一些時間才真正達到目標。 我認為沒有辦法為評論添加功勞,但請告訴我是否存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM