customize the Border property with CornerRadius for ListBox

隐身守侯 提交于 2019-12-04 00:47:49

问题


I want to customize the following Listbox-display property of border with CornerRadius=5..can anyone help me to achieve it without changing the existing datatemplate code in the following Xaml code:

<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>

回答1:


If you want the Border within the ListBoxItems to have another CornerRadius value, you can either re-template ListBoxItem where the Border is defined, or set it implicitly in the ItemContainerStyle Resources

<ListBox ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <Style TargetType="Border">
                    <Setter Property="CornerRadius" Value="5"/>
                </Style>
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
    <!--...-->
</ListBox>

Edit: If you want to set CornerRadius for the ListBox, you can do the same but in Resources instead

    <ListBox ...>
        <ListBox.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="10"/>
            </Style>
        </ListBox.Resources>
    <!--...-->
</ListBox>


来源:https://stackoverflow.com/questions/7175008/customize-the-border-property-with-cornerradius-for-listbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!