【问题标题】:How to use an ItemTemplate on a templated control?如何在模板化控件上使用 ItemTemplate?
【发布时间】:2012-10-18 16:14:11
【问题描述】:

我正在尝试基于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);
    }
}

generic.xaml

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

输出:

【问题讨论】:

    标签: c# wpf silverlight xaml datatemplate


    【解决方案1】:

    我现在想通了。这是我的做法:

    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);    
        }    
    }
    

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多