2011-07-28 35 views
1

我想爲我的ItemsControl使用單選模式。所以我改變了我的ItemsControl到一個ListBox,但是我收到一個異常「用於'ContentPresenter'類型的樣式不能用於輸入'ListBoxItem'。」這是怎麼回事?我不能使用ContentPresenter的ListBoxes?ContentPresenter與列表框

<ListBox SelectionMode="Single" ItemsSource="{Binding}" MinHeight="200" MinWidth="200"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid></UniformGrid> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 

    <ListBox.ItemContainerStyle> 
     <Style TargetType="ContentPresenter"> 
      <Setter Property="Grid.Row" Value="{Binding X}" /> 
      <Setter Property="Grid.Column" Value="{Binding Y}" /> 
     </Style> 
    </ListBox.ItemContainerStyle> 

    <ListBox.ItemTemplate> 
     <DataTemplate DataType="ObservableCollection"> 
      <Border BorderBrush="Black" BorderThickness="0.5" Background="Beige"> 
       <clr:Cell Content="{Binding Character}"></clr:Cell> 
      </Border> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

如果你的問題與Visual Studio本身無關,那就不要使用標籤。 –

回答

5

樣式將需要目標ListBoxItem,因爲這總是創建。可以找到更多的ListBox模板和數據模板文檔和示例here

+0

據我可以告訴第一句回答這個問題,而第二行模糊了這個問題(也許這個例子改變了?)。我將用一個實際的示例'