簡體   English   中英

如何在模板控件上使用ItemTemplate?

[英]How to use an ItemTemplate on a templated control?

我試圖基於ListBox創建自定義模板ListBox以僅顯示控件集合中的單個選定項目。 為此,我定義了一個模板,該模板的ContentPresenter綁定到控件的SelectedItem屬性。 如方案1所示,當為控件提供UIElement集合時,它可以很好地工作。

但是當需要使用DataTemplate來顯示實體對象時, ItemTemplate被忽略,因為控件具有一個Template ,這將阻止使用`ItemTemplate。 根據我的閱讀,這是設計使然,這是有道理的。

但是,如何將DataTemplates用於控件的ItemTemplate並保留控件的默認模板?

我嘗試覆蓋PrepareContainerForItemOverride ,但許多不同的模板配置無濟於事。

這是我嘗試過的一些方法:

<UserControl.Resources>

    <local:StringCollection x:Key="MyColors">
        <sys:String>Yellow</sys:String>
        <sys:String>Purple</sys:String>
    </local:StringCollection>

</UserControl.Resources>

<StackPanel Background="White">

    <TextBlock Margin="0,25,0,0">Scenario 1: Providing UIElements to the
        ControlTemplate's ContentPresenter: Works</TextBlock>
    <control:RotatingFlipView x:Name="Works" SelectedIndex="1">
        <control:RotatingFlipView.Template>
            <ControlTemplate>
                <ContentPresenter
                     Content="{Binding ElementName=Works, Path=SelectedItem}"/>
            </ControlTemplate>
        </control:RotatingFlipView.Template>
        <Rectangle Height="100" Width="100" Fill="Red"/>
        <Rectangle Height="100" Width="100" Fill="Blue"/>
        <Rectangle Height="100" Width="100" Fill="Green"/>
    </control:RotatingFlipView>

    <TextBlock Margin="0,25,0,0">Scenario 2: The ItemTemplate provided is ignored in
        favor of the RotatingFlipView's Template which displays the source as raw
        Strings</TextBlock>
    <control:RotatingFlipView x:Name="Broken"
                              ItemsSource="{StaticResource MyColors}">
        <control:RotatingFlipView.Template>
            <ControlTemplate>
                <ContentPresenter
                    Content="{Binding ElementName=Broken, Path=SelectedItem}"/>
            </ControlTemplate>
        </control:RotatingFlipView.Template>
        <control:RotatingFlipView.ItemTemplate>
            <DataTemplate>
                <Rectangle Height="100" Width="100" Fill="{Binding}"/>
            </DataTemplate>
        </control:RotatingFlipView.ItemTemplate>
    </control:RotatingFlipView>

    <TextBlock Margin="0,25,0,0">Scenario 3: Removing the RotatingFlipView's
        Template, causes the display to fall back on the ListBox's Template,
        though now my ItemTemplate is used:</TextBlock>
    <control:RotatingFlipView x:Name="Broken2"
                              ItemsSource="{StaticResource MyColors}">
        <control:RotatingFlipView.ItemTemplate>
            <DataTemplate>
                <Rectangle Height="100" Width="100" Fill="{Binding}"/>
            </DataTemplate>
        </control:RotatingFlipView.ItemTemplate>
    </control:RotatingFlipView>

</StackPanel>

RotatingFlipView.cs

public class RotatingFlipView : ListBox
{
    public RotatingFlipView()
    {
        DefaultStyleKey = typeof(RotatingFlipView);
    }
}

泛型

<Style TargetType="local:RotatingFlipView">
    <Setter Property="SelectionMode" Value="Single"/>
    <Setter Property="SelectedIndex" Value="0"/>
</Style>

輸出: 輸出量

我現在想通了。 這是我的操作方法:

Mainpage.xaml

<UserControl.Resources>

    <local:StringCollection x:Key="MyColors">
        <sys:String>Yellow</sys:String>
        <sys:String>Purple</sys:String>
    </local:StringCollection>

</UserControl.Resources>

<StackPanel Background="White">
    <control:RotatingFlipView>
        <Rectangle Height="100" Width="100" Fill="Red"/>
        <Rectangle Height="100" Width="100" Fill="Green"/>
        <Rectangle Height="100" Width="100" Fill="Blue"/>
    </control:RotatingFlipView>

    <control:RotatingFlipView ItemsSource="{StaticResource MyColors}">
        <control:RotatingFlipView.ItemTemplate>
            <DataTemplate>
                <Rectangle Height="100" Width="100" Fill="{Binding}"/>
            </DataTemplate>
        </control:RotatingFlipView.ItemTemplate>
    </control:RotatingFlipView>
<StackPanel/>

RotatingFlipView.cs

public class RotatingFlipView : ListBox    
{    
    public RotatingFlipView()    
    {    
        DefaultStyleKey = typeof(RotatingFlipView);    
    }    
}

泛型

<Style TargetType="local:RotatingFlipView">
    <Setter Property="SelectionMode" Value="Single"/>
    <Setter Property="SelectedIndex" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:RotatingFlipView">

                <ContentPresenter
                    Content="{TemplateBinding SelectedItem}"
                    ContentTemplate="{TemplateBinding ItemTemplate}"/>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

暫無
暫無

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

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