C# WPF中列表框的正确样式

C# WPF中列表框的正确样式,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我现在正在努力使用XAML设计我的列表框,因为当我应用我的样式时,列表框中的所有内容都消失了。我正在采用这种风格: <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}"> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="Template"> <Setter.Va

我现在正在努力使用XAML设计我的列表框,因为当我应用我的样式时,列表框中的所有内容都消失了。我正在采用这种风格:

<Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="#2a82bb" BorderThickness="1" Background="White">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

是否必须在content presenter标记中添加一些内容,或者我是否还需要单独设置每个列表项的样式,因为我正在覆盖列表框的默认样式?任何示例都很好,因为MSDN网站上的示例根本没有解释。

ListBox
是项控件,因此当您创建模板而不是
ContentPresenter
时,请使用:


试试这个

<Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                    <Border BorderBrush="#2a82bb" BorderThickness="1" Background="White"/>                       
                    <ContentPresenter/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


当设置ListBoxItem的样式时,我是否也需要ItemsPresenter位于该控件模板内?当您为
ItemsControl
创建
控件模板时,例如
ListBox
,您可以使用
ItemsPresenter
来说明项目应在何处呈现,以及
ContentControl
的时间。您可以使用
ContentPresenter
来说明内容部分应在何处呈现。
<ControlTemplate>
   <Border BorderBrush="#2a82bb" BorderThickness="1" Background="White">
      <ItemsPresenter/>
   </Border>
</ControlTemplate>
<Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                    <Border BorderBrush="#2a82bb" BorderThickness="1" Background="White"/>                       
                    <ContentPresenter/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>