简体   繁体   English

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

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

I am trying to create custom templated control based on a ListBox to display only the single selected item in the control's collection. 我试图基于ListBox创建自定义模板ListBox以仅显示控件集合中的单个选定项目。 To this aim, I have defined a Template with an ContentPresenter , databound to the SelectedItem property of the control. 为此,我定义了一个模板,该模板的ContentPresenter绑定到控件的SelectedItem属性。 As illustrated in scenario 1, it works well when providing the control with a collection of UIElement . 如方案1所示,当为控件提供UIElement集合时,它可以很好地工作。

But when needing to use a DataTemplate to display an entity object, the ItemTemplate is ignored because the control has a Template which will prevent the `ItemTemplate from being used. 但是当需要使用DataTemplate来显示实体对象时, ItemTemplate被忽略,因为控件具有一个Template ,这将阻止使用`ItemTemplate。 From what I've read this is by design and it sorts of makes sense. 根据我的阅读,这是设计使然,这是有道理的。

But how can I use DataTemplates for my control's ItemTemplate and retain my control default template? 但是,如何将DataTemplates用于控件的ItemTemplate并保留控件的默认模板?

I have tried overriding PrepareContainerForItemOverride , and many different template configurations to no avail. 我尝试覆盖PrepareContainerForItemOverride ,但许多不同的模板配置无济于事。

Here is some of what I tried: 这是我尝试过的一些方法:

<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 RotatingFlipView.cs

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

generic.xaml 泛型

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

Output: 输出: 输出量

I have now figured it out. 我现在想通了。 Here is how I've done it: 这是我的操作方法:

Mainpage.xaml 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 RotatingFlipView.cs

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

generic.xaml 泛型

<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.

相关问题 在ItemTemplate Silverlight中访问模板化列表框 - Access Templated Listbox in ItemTemplate Silverlight 如何找到位于datalist itemtemplate内的控件 - How to find a control located inside datalist itemtemplate 如何使用模板委托 - How to use templated delegate 如何打包自定义 UWP 模板化控件以在其他项目中重复使用,并可能供其他人使用? - How do I package a custom UWP templated control for reuse in other projects, and maybe for others to use? 如何正确使用列表框itemtemplate / datatemplate中的按钮? - How to correctly use a button in a listbox itemtemplate / datatemplate? 如何对模板控件中的按钮单击做出反应 - How to react on a button click in a Templated Control 带有自定义控件的MVVM ItemTemplate - MVVM ItemTemplate with custom control 如何在UWP中更改选择后在Pivot.Itemtemplate中获取控件? - How to get the a control inside a Pivot.Itemtemplate on selection changed in UWP? 如何绑定数据列表控件的itemtemplate字段内的dropdownlist - how to bind dropdownlist which is inside the itemtemplate field of datalist control 如何知道 GridView 的 ItemTemplate 中服务器控件的 ID? - how to know the id of server control inside the ItemTemplate of a GridView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM