2012-10-18 36 views
0

我正在嘗試基於ListBox創建自定義模板控件,以僅顯示控件集合中的單個選定項目。 爲了達到這個目的,我用ContentPresenter定義了一個模板,該控件的SelectedItem屬性的數據綁定。 如方案1所示,在爲控件提供UIElement集合時,效果很好。如何在模板控件上使用ItemTemplate?

但是需要使用一個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> 

輸出: Output

回答

0

我現在想通了。這裏是如何我已經做到了:

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>