【问题标题】:Binding selectedItem ListBox of Radio buttons in combobox popup在组合框弹出窗口中绑定单选按钮的 selectedItem ListBox
【发布时间】:2011-09-07 16:32:46
【问题描述】:

好的,所以我的问题有点复杂,所以我会尽量说清楚。因为单选按钮的集合没有“SelectedItem/Value”属性,所以我根据我在网上找到的一些 XAML 将我的单选按钮收集到了一个 ListBox 中。现在,为了节省 UI 上的空间,ListBox 正在接管 ComboBox 弹出窗口的内容。 (因此,当您单击组合框时,下拉的是 WrapPanel 中的单选按钮列表。)我已经能够成功地将 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" />

【问题讨论】:

    标签: wpf binding controltemplate


    【解决方案1】:

    我有点困惑,为什么要先覆盖 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>
    

    【讨论】:

    • 我的最终目标是让单选按钮在某些情况下自己出现,然后在其他情况下隐藏在组合框中。我试图用相同的风格来实现这一点,但我看不出有什么理由不能用两种不同的模型来做到这一点。将单选按钮放入包装面板中可能需要一些工作,而不仅仅是垂直列表,但我认为我可以在列表中使用它们。非常感谢!
    • @AceGambit ComboBoxItem 继承自 ListBoxItem,因此您可以为 ListBox 和 ComboBox 重复使用相同的 ItemContainerStyle
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2011-06-24
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多