簡體   English   中英

在集合內綁定控件

[英]Binding controls inside a collection

我有一個可互換視圖的WPF應用程序。 一個視圖繼承自我的ViewBase類:

public class ViewBase : ContentControl, IView
{
    public ViewBase()
    {
        ToolbarGroups = new ObservableCollection<ToolbarGroup>();
    }

    public ObservableCollection<ToolbarGroup> ToolbarGroups
    {
        get { return (ObservableCollection<ToolbarGroup>)GetValue(ToolbarGroupProperty); }
        private set { SetValue(ToolbarGroupProperty, value); }
    }

    public static readonly DependencyProperty ToolbarGroupProperty = 
                DependencyProperty.Register("ToolbarGroup", typeof(ObservableCollection<ToolbarGroup>), typeof(ViewBase));
}

我將ToolbarGroups屬性綁定到窗口中的ItemsControl。 效果很好。

ToolBarGroup繼承自HeaderedContentControl,我將它們連接在這樣的視圖中:

<base:ViewBase x:Class="TestApp.Orders.OrderDetailsView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:base="clr-namespace:TestApp"
               xmlns:local="clr-namespace:TestApp.Orders"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               x:Name="root">

    <base:ViewBase.ToolbarGroups>
        <base:ToolbarGroup Header="Group1">
            <!-- Buttons and other controls go here -->
        </base:ToolbarGroup>

        <!-- Binding to MyProp is the problem -->
        <local:PremadeToolbarGroup MyProp="{Binding}" />

        <!-- This also doesn't work -->
        <local:PremadeToolbarGroup MyProp="{Binding ElementName=root,Path=DataContext}" />
    </base:ViewBase.ToolbarGroups>

        <Grid>
            <!-- this binding works -->
            <TextBox Text="{Binding Path=Model.FullName}" />
        </Grid>
</base:ViewBase>

PremadeToolbarGroup是一個XAML文件,它繼承自ToolbarGroup並添加了一些按鈕。 PremadeToolbarGroup包含一個名為MyProp的依賴項屬性,我想將該屬性綁定到其使用的視圖的DataContext。

My DataContext設置為ViewModelBase類型的對象,這也是MyProp依賴項屬性的類型。

PremadeToolbarGroup會按預期顯示,但是綁定永遠不會起作用(MyProp始終為null)。 有什么想法嗎?

是否在任何地方設置了DataContext? 嘗試將DataContext = this添加到構造函數中。

我找到了解決方案:

在我的窗口中,該視圖顯示在ContentPresenter中:

<ContentPresenter Margin="3" x:Name="ViewPresenter" />

通過隱藏代碼將content屬性設置為View。

正如我在問題中所說的:“我將ToolbarGroups屬性綁定到窗口中的ItemsControl。”

因此,我在此窗口的其他位置顯示了ToolbarGroups:

<ItemsControl ItemsSource="{Binding ElementName=ViewPresenter, Path=Content.ToolbarGroups}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

ToolbarGroup對象無法綁定到View的DataContext,因為它們已添加到視圖外部的可視樹中。

我可以通過將ItemsControl中每個項目的DataContext綁定到ViewPresetner內容的DataContext來修復它。 看起來像這樣:

<ItemsControl ItemsSource="{Binding ElementName=ViewPresenter, Path=Content.ToolbarGroups}">
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="FrameworkElement.DataContext" Value="{Binding ElementName=ViewPresenter,Path=Content.DataContext }"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

有點混亂,但可以。 如果有人有,我當然願意接受更優雅的解決方案。

暫無
暫無

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

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