使用CornerRadius为ListBox自定义Border属性

som*_*har 9 wpf xaml listbox border cornerradius

我想使用CornerRadius = 5来自定义边框的以下Listbox-display属性.任何人都可以帮助我实现它而无需更改以下Xaml代码中的现有datatemplate代码:

<ListBox x:Uid="lst_value"  Name="lstValues" Background="Wheat" BorderBrush="Black"
         HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="1" Height="100" Width="150"
         ItemsSource="{Binding listval}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" Background="{Binding}">
                <TextBlock x:Name="txtblk" Foreground="Black" FontSize="10"  TextAlignment="Left" 
                                               FontWeight="Black" Text="{Binding}" Background="{Binding}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

Fre*_*lad 14

如果你想在Border内部ListBoxItems有另一个CornerRadius值,您可以重新模板ListBoxItem,其中Border定义,或隐式设置它ItemContainerStyle的资源

<ListBox ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <Style TargetType="Border">
                    <Setter Property="CornerRadius" Value="5"/>
                </Style>
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
    <!--...-->
</ListBox>
Run Code Online (Sandbox Code Playgroud)

编辑:如果你想设置CornerRadiusListBox,你可以做相同的,但在Resources替代

    <ListBox ...>
        <ListBox.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="10"/>
            </Style>
        </ListBox.Resources>
    <!--...-->
</ListBox>
Run Code Online (Sandbox Code Playgroud)