我對WPF世界很陌生,我在ItemsControl中遇到了模板項目的一些問題。我需要的是在ItemsControl(或類似的東西)中模板元素(主要是按鈕)。ItemTemplate:ListBox vs ItemsControl
如果我用下面的XAML代碼...
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Border BorderBrush="AliceBlue"
BorderThickness="3">
<TextBlock Text="Templated!"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<Button>Not templated</Button>
<TextBlock>Text not templated</TextBlock>
</ItemsControl>
...我得到這樣的結果:http://img444.imageshack.us/img444/2167/itemscontrolnottemplate.gif
的的ItemsControl模板並不適用於無論是按鈕也不對TextBlock控制。如果我改變ItemsControl的成列表框這樣的:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Border BorderBrush="AliceBlue"
BorderThickness="3">
<TextBlock Text="Templated!"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<Button>Not templated</Button>
<TextBlock>Text not templated</TextBlock>
</ListBox>
...然後我得到這個結果:img814.imageshack.us/img814/6204/listboxtoomuchtemplatin.gif
現在該模板適用於BOTH子控件(即使我將數據類型設置爲僅限於)。