C# 具有列表框的ContentPresenter
我正在尝试对ItemsControl使用单一选择模式。因此,我将ItemsControl更改为ListBox,但出现了一个异常 “无法将用于“ContentPresenter”类型的样式应用于 键入“ListBoxItem”。” 上面是什么?我不能对列表框使用ContentPresenter吗C# 具有列表框的ContentPresenter,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试对ItemsControl使用单一选择模式。因此,我将ItemsControl更改为ListBox,但出现了一个异常 “无法将用于“ContentPresenter”类型的样式应用于 键入“ListBoxItem”。” 上面是什么?我不能对列表框使用ContentPresenter吗 <ListBox SelectionMode="Single" ItemsSource="{Binding}" MinHeight="200" MinWidth="200"> <
<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>
样式需要以ListBoxItem为目标,因为它总是被创建的。可以找到更多的ListBox模板和datatemplate文档和示例。如果您的问题与Visual Studio本身无关,请不要使用标记。据我所知,第一句回答了问题,而第二行混淆了问题(可能示例已更改?)。我将用一个实际的例子删除第二句话
@OmegaMan,URL指向一个带有DataTemplate例子的ListBox例子。我不明白您为什么认为这会混淆问题。用户示例中的ItemTemplate
中有一个DataTemplate
,链接示例中也显示了该示例。因此,我(至少)不清楚为什么会给出DataTemplate
的建议来解决需要更改样式的问题。