簡體   English   中英

UserControl中的ItemTemplate Wrapper基於ItemsControl

[英]ItemTemplate Wrapper in UserControl Based on ItemsControl

我正在開發自己的用戶控件,它來自ItemsControl。 簡要說明我想要實現的目標是XAML現在的樣子:

<ItemsControl x:Class="MyApp.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyApp"
             mc:Ignorable="d"
             d:DesignHeight="60" d:DesignWidth="600">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Template>
        <ControlTemplate>
            <Grid>
                <Border Background="{TemplateBinding Background}" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ItemsPresenter/>
                </Border>
            </Grid>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

這以我想要的方式呈現我的數據,在水平的StackPanel中,沒什么特別的。 在可視化樹中,您將看到:

  ItemsControl
    [...Other Visual Item in The Tree...]
    ItemsPresenter
      ContentPresenter
         Item1 (ItemTemplate)
      ContentPresenter
         Item2 (ItemTemplate)

現在我想修改我的用戶控件的XAML,以便樹看起來像這樣:

  ItemsControl
    [...Other Visual Item in The Tree...]
    ItemsPresenter
      ContentPresenter
         SomeContainerDefinedInMyUserControlXAML
           Item1 (ItemTemplate)
      ContentPresenter
         SomeContainerDefinedInMyUserControlXAML
           Item2 (ItemTemplate)

這里的目標是在模板化項目周圍有一個包裝容器。 它的行為將綁定到UserControl的內部屬性,允許我定義項目行為,不受我的控件用戶做出的ItemTemplate選項的影響。

我試圖在ItemsControl.Resources中的ContentPresenter上添加DataTemplates,但是失敗了。 有人可以幫幫我嗎? :)

您可以在ItemControl的Get Item For Item Override方法中創建派生的ContentPresenter或ContentControl:

public class MyContainer : ContentControl
{
}

public class MyItemsControl : ItemsControl
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new MyContainer();
    }

    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return item is MyContainer;
    }
}

我還建議將派生的ItemsControl的XAML移動到Themes\\Generic.xaml的默認樣式(默認情況下對自定義控件執行):

<Style TargetType="local:MyItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MyItemsControl">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ItemsPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

還添加

static MyItemsControl()
{
    DefaultStyleKeyProperty.OverrideMetadata(
        typeof(MyItemsControl),
        new FrameworkPropertyMetadata(typeof(MyItemsControl)));
}

到控件的代碼。

暫無
暫無

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

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