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