2009-03-19 19 views
4

UserControl私はUIElementのリストを表示するために使用しています。コントロールはそれで一つのItemsControlで構成されていItemPanelTemplateは、そのItemsSourceUserControlUserControl.ResourcesでそのItemTemplateセットによって公開されDependencyPropertyにバインドされた、水平StackPanelのために切り替えています。WPF - ItemTemplateが期待どおりに動作しない

ItemTemplateが適用されないのを除いてすべて正常に動作し、理由がわかりません。完全な情報源は以下の通りです。

UserControl.xaml -

<UserControl x:Name="UC" x:FieldModifier="private" x:Class="ContentSliderControl.ContentSlider" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<UserControl.Resources> 

    <DataTemplate x:Key="pageTemplate"> 
     <Border CornerRadius="10" Padding="5" Height="200" Width="200" Background="#333"> 
      <ContentControl Content="{Binding}"/> 
     </Border> 
    </DataTemplate> 

    <ItemsPanelTemplate x:Key="template"> 
     <StackPanel IsItemsHost="True" 
      Orientation="Horizontal" 
      ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
      ScrollViewer.VerticalScrollBarVisibility="Disabled"/> 
    </ItemsPanelTemplate> 
</UserControl.Resources> 

<ItemsControl ItemsPanel="{StaticResource template}" 
       ItemTemplate="{StaticResource pageTemplate}" 
       ItemsSource="{Binding ElementName=UC,Path=Pages}"/> 

UserControl.xaml.cs -

[ContentProperty("Pages")] 
public partial class ContentSlider : UserControl 
{ 


    public List<UIElement> Pages 
    { 
     get { return (List<UIElement>)GetValue(PagesProperty); } 
     //set { SetValue(PagesProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Pages. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty PagesProperty = 
     DependencyProperty.Register("Pages", typeof(List<UIElement>), typeof(ContentSlider), new UIPropertyMetadata(null)); 



    public ContentSlider() 
    { 
     InitializeComponent(); 
    } 
} 

}

私はこのような私のメインウィンドウに制御を消費 -

<slider:ContentSlider > 
    <slider:ContentSlider.Pages> 
     <Button>1</Button> 
     <Button>2</Button> 
     <Button>3</Button> 
     <Button>4</Button> 
    </slider:ContentSlider.Pages> 
</slider:ContentSlider> 

ボタンは正常に表示されますが、200ピクセルの四角形の枠内には表示されません。

どんな助けもgreatlly appriciatedされるでしょう。おかげさまで

答えて

4

UIElementのリストなので、アイテムテンプレートはアイテムを直接表示できない場合にのみ適用されます。

+0

あなたは何をお勧めしますか? – Stimul8d

+0

また、リストボックスのItemsControlを交換すると、予想通りにレンダリングされるので、あなたがそこにいるかどうかわからない – Stimul8d

+0

私の言葉を食べなければならないようですね!ありがとうございました。 – Stimul8d

6

Nirが正しい場合、ItemsControlは、UIElementsの場合はPanelに直接項目を追加します。私は、MSDNで、この動作の一切の言及を見つけることができなかったが、博士WPFはhis article on item containersでそれを言及:

のUIElementが明示的なのItemsControlインスタンスのItemsコレクションに追加された場合(のインスタンスとは対照的に、 ListBoxのような派生クラス)、アイテムパネルの直接の子になります。非UIElementが追加された場合は、ContentPresenter内でラップされます。

あなたの解決策ではなくListBoxを使用することはおそらくあり、そしてListBoxItemのための新しいStyleItemContainerStyleを設定し、そのスタイルで、その中にあなたのBorderControlTemplateを使用しています。

+0

まあ、それはいくつかの狂った行動ではありません。ロバートのおかげで、私はNirに正解を与えて投票します。あなたは1つ以上の正解を持つことはできませんええ? – Stimul8d

-1

DataTemplateDataTypeプロパティを設定すると、動作が開始されます。

public class ItemsControlForUIElement : ItemsControl 
{ 
    protected override DependencyObject GetContainerForItemOverride() 
    { 
     return new ContentPresenter(); 
    } 
    protected override bool IsItemItsOwnContainerOverride(object item) 
    { 
     return false; 
    } 
} 
4

ニールは、このカスタムItemsControlに実装しても問題が解決し、独自のItemTemplateにを使用できるようになる、権利です。彼の解決策は、与えられたシナリオでは過度の制御テンプレートを使用することです。または、ItemContainerStyleListBoxItemの新しいスタイルに設定し、ContentTemplateListBoxItemTemplateに使用するDataTemplateに設定して、を使用します。

2

ロバートMacneeが頭の上にその理由を釘付け:

関連する問題