在组合框弹出窗口中绑定单选按钮的 selectedItem 列表框

发布于 2024-12-03 13:10:43 字数 5111 浏览 0 评论 0原文

好吧,我的问题有点复杂,所以我会尽量说得清楚。由于单选按钮的集合没有“SelectedItem/Value”属性,因此我根据网上找到的一些 XAML 将单选按钮收集到了 ListBox 中。现在,为了节省 UI 空间,该 ListBox 正在接管 ComboBox 弹出窗口的内容。 (因此,当您单击组合框时,下拉的是 WrapPanel 中的单选按钮列表。)我已经能够成功地将 RadioButton 列表框控件本身绑定到项目源并将所选值绑定到属性,但是一旦进入组合框,我似乎无法使选定的值绑定正常工作(ItemsSource 仍然可以正常工作)。

<Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel Background="Transparent" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="5" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border BorderThickness="1" Background="Transparent">
                                <RadioButton Focusable="False" Width="120"
                                        IsHitTestVisible="False"
                                        Content="{Binding Name}"
                                        IsChecked="{TemplateBinding IsSelected}">
                                </RadioButton>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border BorderThickness="0" Padding="0" BorderBrush="Transparent" Background="Transparent" Name="Bd" SnapsToDevicePixels="True">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="RadioButtonCombo" TargetType="ComboBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <ToggleButton 
                            Name="ToggleButton"
                            DataContext="{TemplateBinding ItemsSource}"
                            Content="{Binding ElementName=DropDownContent, Path=SelectedItem.Name}"
                            Focusable="false"
                            IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                            ClickMode="Press" />
                    <ContentPresenter
                            Name="ContentSite"
                            IsHitTestVisible="False" 

                            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                            Margin="3,3,23,3"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left" />
                    <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}"
                            AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
                        <Grid Name="DropDown" SnapsToDevicePixels="True"
                              MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border x:Name="DropDownBorder" Background="WhiteSmoke"
                                    BorderThickness="1" BorderBrush="Black"/>
                            <ListBox Name="DropDownContent" Style="{StaticResource RadioButtonList}" 
                                     Width="{TemplateBinding MaxWidth}"
                                     SelectedValue="{Binding Path=SelectedValue, RelativeSource={RelativeSource TemplatedParent}}"
                                     ItemsSource="{TemplateBinding ItemsSource}">
                            </ListBox>
                        </Grid>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
    </Style.Triggers>
</Style>

我的使用方式如下:

    <ComboBox Name="SourceComboCtl" 
              Text="Source" 
              SelectedValuePath="CodeId" 
              DisplayMemberPath="Name" 
              SelectedValue="{Binding Path=SourceCode}"
              Style="{StaticResource RadioButtonCombo}"
              Width="60" 
              MaxWidth="150" />

Ok, so my question is kind of convoluted, so I'll try to be as clear as possible. Because a collection of radio buttons does not have a 'SelectedItem/Value' property, I have collected my radioButtons into a ListBox, based on some XAML I found online. Now, in order to save space on the UI, that ListBox is taking over the content of a ComboBox popup. (So when you click the comboBox, what drops down is a list of radiobuttons in a WrapPanel.) I have been able to successfully bind the RadioButton listbox control by itself to an itemssource and bind selected value to a property, but once inside the combo box, I cannot seem to get the selected value binding to work properly, (ItemsSource still works fine).

<Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel Background="Transparent" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="5" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border BorderThickness="1" Background="Transparent">
                                <RadioButton Focusable="False" Width="120"
                                        IsHitTestVisible="False"
                                        Content="{Binding Name}"
                                        IsChecked="{TemplateBinding IsSelected}">
                                </RadioButton>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border BorderThickness="0" Padding="0" BorderBrush="Transparent" Background="Transparent" Name="Bd" SnapsToDevicePixels="True">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="RadioButtonCombo" TargetType="ComboBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <ToggleButton 
                            Name="ToggleButton"
                            DataContext="{TemplateBinding ItemsSource}"
                            Content="{Binding ElementName=DropDownContent, Path=SelectedItem.Name}"
                            Focusable="false"
                            IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                            ClickMode="Press" />
                    <ContentPresenter
                            Name="ContentSite"
                            IsHitTestVisible="False" 

                            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                            Margin="3,3,23,3"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left" />
                    <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}"
                            AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
                        <Grid Name="DropDown" SnapsToDevicePixels="True"
                              MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border x:Name="DropDownBorder" Background="WhiteSmoke"
                                    BorderThickness="1" BorderBrush="Black"/>
                            <ListBox Name="DropDownContent" Style="{StaticResource RadioButtonList}" 
                                     Width="{TemplateBinding MaxWidth}"
                                     SelectedValue="{Binding Path=SelectedValue, RelativeSource={RelativeSource TemplatedParent}}"
                                     ItemsSource="{TemplateBinding ItemsSource}">
                            </ListBox>
                        </Grid>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
    </Style.Triggers>
</Style>

My use of that looks like this:

    <ComboBox Name="SourceComboCtl" 
              Text="Source" 
              SelectedValuePath="CodeId" 
              DisplayMemberPath="Name" 
              SelectedValue="{Binding Path=SourceCode}"
              Style="{StaticResource RadioButtonCombo}"
              Width="60" 
              MaxWidth="150" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-12-10 13:10:43

我有点困惑为什么要首先覆盖 ListBox 以显示 RadioButtons,然后覆盖 ComboBox 以使用 ListBox 显示 RadioButtons

为什么不直接覆盖 ComboBox.ItemTemplate 并完全跳过 ListBox? ComboBox 也有 SelectedItem/Value

这是我的 RadioButtonListBox 样式。我只是将 ListBox 的位置更改为 ComboBox

<Style x:Key="RadioButtonComboBoxStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ComboBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <RadioButton IsHitTestVisible="False" Focusable="false" 
                                    Content="{TemplateBinding ContentPresenter.Content}"  
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

I'm a bit confused why you'd first overwrite a ListBox to display RadioButtons, then overwrite a ComboBox to use a ListBox to display RadioButtons

Why not just overwrite ComboBox.ItemTemplate and skip the ListBox alltogether? ComboBox has SelectedItem/Value too

Here's my RadioButtonListBox Style. I simply changed where it says ListBox to say ComboBox

<Style x:Key="RadioButtonComboBoxStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ComboBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <RadioButton IsHitTestVisible="False" Focusable="false" 
                                    Content="{TemplateBinding ContentPresenter.Content}"  
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文