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

时间:2011-09-07 16:32:47

标签: wpf binding controltemplate

好的,所以我的问题有点令人费解,所以我会尽量保持清醒。因为单选按钮的集合没有'SelectedItem / Value'属性,所以我已将我的radioButtons收集到ListBox中,基于我在网上找到的一些XAML。现在,为了节省UI上的空间,ListBox正在接管ComboBox弹出窗口的内容。 (所以当你单击comboBox时,下拉的是WrapPanel中的radiobuttons列表。)我已经能够成功地将RadioButton列表框控件本身绑定到一个itemssource并将选定的值绑定到一个属性,但是一旦在组合内部框,我似乎无法使选定的值绑定工作正常,(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" />

1 个答案:

答案 0 :(得分:2)

我有点困惑为什么你第一次覆盖ListBox来显示RadioButtons,然后覆盖ComboBox以使用ListBox来显示RadioButtons

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

这是我的RadioButtonListBox样式。我只是改变了ListBoxComboBox

的地方
<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>