【问题标题】:customize the Border property with CornerRadius for ListBox使用 CornerRadius 为 ListBox 自定义 Border 属性
【发布时间】:2023-04-10 04:21:01
【问题描述】:

我想用 CornerRadius=5 自定义边框的以下 Listbox-display 属性。任何人都可以帮我实现它,而无需更改以下 Xaml 代码中的现有数据模板代码:

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

【问题讨论】:

    标签: wpf xaml listbox border cornerradius


    【解决方案1】:

    如果您希望ListBoxItems 中的Border 具有另一个CornerRadius 值,您可以在定义Border 的位置重新模板ListBoxItem,或在ItemContainerStyle 资源中隐式设置它

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

    编辑:如果你想为ListBox设置CornerRadius,你可以做同样的事情,但是在Resources

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

    【讨论】:

    • 是的,我们可以在列表框内为每个列表项设置一个边框...因为我需要将带有cornerradius的边框属性应用于整个列表框本身...希望我的清楚问题..
    • 所以你想把ListBox的角转成一个Border,对吗?
    • ListBox 而不是ListBoxItems 更新了我的答案
    • 那是完美的......但是一旦选择列表框中的项目不再具有背景颜色(它只是所选项目的白色背景),除此之外我没有默认列表框行为(即项目的单选和多选)。相反,它们被分别突出显示或选择....对于 WPF 来说是一个全新的...这对我来说会更有帮助...如果您能指导我解决它的方法..谢谢..跨度>
    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多