Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有列表框的ContentPresenter_C#_Wpf_Xaml - Fatal编程技术网

C# 具有列表框的ContentPresenter

C# 具有列表框的ContentPresenter,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试对ItemsControl使用单一选择模式。因此,我将ItemsControl更改为ListBox,但出现了一个异常 “无法将用于“ContentPresenter”类型的样式应用于 键入“ListBoxItem”。” 上面是什么?我不能对列表框使用ContentPresenter吗 <ListBox SelectionMode="Single" ItemsSource="{Binding}" MinHeight="200" MinWidth="200"> <

我正在尝试对ItemsControl使用单一选择模式。因此,我将ItemsControl更改为ListBox,但出现了一个异常

“无法将用于“ContentPresenter”类型的样式应用于 键入“ListBoxItem”。”

上面是什么?我不能对列表框使用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>


样式需要以ListBoxItem为目标,因为它总是被创建的。可以找到更多的ListBox模板和datatemplate文档和示例。

如果您的问题与Visual Studio本身无关,请不要使用标记。据我所知,第一句回答了问题,而第二行混淆了问题(可能示例已更改?)。我将用一个实际的例子删除第二句话
@OmegaMan,URL指向一个带有DataTemplate例子的ListBox例子。我不明白您为什么认为这会混淆问题。用户示例中的
ItemTemplate
中有一个
DataTemplate
,链接示例中也显示了该示例。因此,我(至少)不清楚为什么会给出
DataTemplate
的建议来解决需要更改样式的问题。