如何设置WPF列表框为单选按钮模式?

2026-04-29 07:012阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计437个文字,预计阅读时间需要2分钟。

如何设置WPF列表框为单选按钮模式?

我想将我的一些系列表格项目设置为radiobuttons。这是我的代码,但这种风格适用于每个ListBoxItem。Window.Resources Style TargetType={x:Type ListBoxItem} Setter Property=Template Setter.Value={ControlTemplate TargetType=}

我想将我的一些列表框项目设置为radiobuttons.这是我的代码,但这种风格应用于每个listboxitem.

<Window.Resources> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <RadioButton Content="{TemplateBinding Content}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources>

我该怎么做才能让我可以指定一个列表框来设置单选按钮.我想这个名称会是这样的:

<ListBox Name="ListBox1" Width="120" Visibility="Visible" Background="{x:Null}" BorderThickness="0" Style="{StaticResource radioListBox}">

我知道问题的一部分是这只是样式listboxitems但我不知道如何设置列表框本身的样式.我当然更喜欢添加背景和边框属性.

任何帮助,将不胜感激.

如何设置WPF列表框为单选按钮模式?

你需要给你的风格一把钥匙:

<Window.Resources> <Style x:Key="radioListBoxItem" TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <RadioButton Content="{TemplateBinding Content}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources>

然后你将它应用于ListBox,如:

<ListBox ItemContainerStyle="{StaticResource radioListBoxItem}" />

如果要为包含无线电列表框项和其他一些属性的ListBox创建样式,您也可以这样做:

<Style x:Key="radioListBox" TargetType="{x:Type ListBox}"> <Setter Property="ItemContainerStyle" Setter="{StaticResource radioListBoxItem}" /> <Setter Property="Background" Value="Navy" /> </Style>

你会申请它:

<ListBox Style="{StaticResource radioListBox}" />

本文共计437个文字,预计阅读时间需要2分钟。

如何设置WPF列表框为单选按钮模式?

我想将我的一些系列表格项目设置为radiobuttons。这是我的代码,但这种风格适用于每个ListBoxItem。Window.Resources Style TargetType={x:Type ListBoxItem} Setter Property=Template Setter.Value={ControlTemplate TargetType=}

我想将我的一些列表框项目设置为radiobuttons.这是我的代码,但这种风格应用于每个listboxitem.

<Window.Resources> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <RadioButton Content="{TemplateBinding Content}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources>

我该怎么做才能让我可以指定一个列表框来设置单选按钮.我想这个名称会是这样的:

<ListBox Name="ListBox1" Width="120" Visibility="Visible" Background="{x:Null}" BorderThickness="0" Style="{StaticResource radioListBox}">

我知道问题的一部分是这只是样式listboxitems但我不知道如何设置列表框本身的样式.我当然更喜欢添加背景和边框属性.

任何帮助,将不胜感激.

如何设置WPF列表框为单选按钮模式?

你需要给你的风格一把钥匙:

<Window.Resources> <Style x:Key="radioListBoxItem" TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <RadioButton Content="{TemplateBinding Content}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources>

然后你将它应用于ListBox,如:

<ListBox ItemContainerStyle="{StaticResource radioListBoxItem}" />

如果要为包含无线电列表框项和其他一些属性的ListBox创建样式,您也可以这样做:

<Style x:Key="radioListBox" TargetType="{x:Type ListBox}"> <Setter Property="ItemContainerStyle" Setter="{StaticResource radioListBoxItem}" /> <Setter Property="Background" Value="Navy" /> </Style>

你会申请它:

<ListBox Style="{StaticResource radioListBox}" />