Binding selectedItem ListBox of Radio buttons in combobox popup

时间秒杀一切 提交于 2020-01-23 12:55:05

问题


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

回答1:


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>


来源:https://stackoverflow.com/questions/7337391/binding-selecteditem-listbox-of-radio-buttons-in-combobox-popup

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