gpt4 book ai didi

wpf - WPF 中的数据绑定(bind)单选按钮列表

转载 作者:行者123 更新时间:2023-12-02 03:47:40 25 4
gpt4 key购买 nike

我在数据对象中有一个选项列表,我想制作一个相当于单选按钮列表的选项,以允许用户仅选择其中一个。功能类似于数据绑定(bind)组合框,但采用单选按钮格式。

愚蠢的我,我以为这会是内置的,但没有。你是怎么做到的?

最佳答案

基本上,在查看了谷歌结果后,我从 an MSDN discussion thread where Dr. WPF provided an answer 的信息开始,其中讨论了如何设置 ListBox 使其看起来正确。然而,当列表框被禁用时,背景是一种令人讨厌的颜色,我一生都无法摆脱它,直到我读到 the MSDN example of the ListBox ControlTemplate ,它显示了正在踢我的背景屁股的 secret 边框元素。

所以,这里的最终答案是这种风格:

<Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}">
<!-- ControlTemplate taken from MSDN http://msdn.microsoft.com/en-us/library/ms754242.aspx -->
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="95"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border Name="Border" Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
CornerRadius="2">
<ScrollViewer Margin="0" Focusable="false">
<StackPanel Margin="2" IsItemsHost="True" />
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background"
Value="Transparent" />
<Setter TargetName="Border" Property="BorderBrush"
Value="Transparent" />
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Name="theBorder" Background="Transparent">
<RadioButton Focusable="False" IsHitTestVisible="False"
IsChecked="{TemplateBinding IsSelected}">
<ContentPresenter />
</RadioButton>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>

它为列表框和项目提供了 ControlTemplate 和样式。它的使用方式如下:

<ListBox Grid.Column="1" Grid.Row="0" x:Name="TurnChargeBasedOnSelector" Background="Transparent"
IsEnabled="{Binding Path=IsEditing}"
Style="{StaticResource RadioButtonList}"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainForm}}, Path=DataContext.RampTurnsBasedOnList}"
DisplayMemberPath="Description" SelectedValuePath="RampTurnsBasedOnID"
SelectedValue="{Binding Path=RampTurnsBasedOnID, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"/>

我花在 WPF 上的时间越多,我就越觉得它会让琐碎的事情变得异常困难,让琐碎的事情变得异常困难。享受。 -斯科特

关于wpf - WPF 中的数据绑定(bind)单选按钮列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/805015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com