在组合框弹出绑定单选按钮,列表框将selectedItem [英] Binding selectedItem ListBox of Radio buttons in combobox popup
问题描述
好了,我的问题是一种令人费解的,所以我会尽量尽可能明确。由于单选按钮的集合,没有一个'的SelectedItem /值属性,我收集了我的单选按钮变成一个ListBox,基于一些XAML我在网上找到。现在,为了节省用户界面空间,即列表框接管一个组合框弹出的内容。 (因此,当您单击组合框,什么滴下来是一个WrapPanel单选按钮的列表。)我已经能够通过自身成功绑定单选ListBox控件到的ItemsSource和选定值绑定到一个属性,但是一旦组合内中,我似乎无法得到所选择的值绑定正常工作,(的ItemsSource仍然正常工作)。
<风格X:键=单选按钮列表的TargetType ={X:类型列表框}>
< setter属性=背景值=白/>
< setter属性=ItemsPanel>
< Setter.Value>
< ItemsPanelTemplate>
< WrapPanel背景=透明/>
< / ItemsPanelTemplate>
< /Setter.Value>
< /二传手>
< setter属性=ItemContainerStyle>
< Setter.Value>
<风格的TargetType ={X:输入一个ListBoxItem}>
< setter属性=保证金VALUE =5/>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:输入一个ListBoxItem}>
< BORDER了borderThickness =1=背景透明>
&所述;单选调焦=假宽度=120
IsHitTestVisible =假
CONTENT ={绑定名称}
器isChecked ={TemplateBinding IsSelected}>
< /单选>
< /边框>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /Setter.Value>
< /二传手>
< setter属性=Control.Template>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型列表框}>
< BORDER了borderThickness =0填充=0BorderBrush =透明背景=透明NAME =BDSnapsToDevicePixels =真>
<项目presenter SnapsToDevicePixels ={TemplateBinding UIElement.SnapsToDevicePixels}/>
< /边框>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
<风格X:键=RadioButtonCombo的TargetType =组合框>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType =组合框>
<网格和GT;
<切换按钮
NAME =切换按钮
的DataContext ={TemplateBinding的ItemsSource}
CONTENT ={绑定的ElementName = DropDownContent,路径= SelectedItem.Name}
调焦=假
器isChecked ={绑定路径= IsDropDownOpen,模式=双向,的RelativeSource = {的RelativeSource TemplatedParent}}
ClickMode =preSS/>
<内容presenter
NAME =ContentSite
IsHitTestVisible =假 的ContentTemplate ={TemplateBinding SelectionBoxItemTemplate}
ContentTemplateSelector ={TemplateBinding ItemTemplateSelector}
保证金=3,3,23,3
VerticalAlignment =中心
的HorizontalAlignment =左/>
<弹出NAME =弹出式布局=底ISOPEN ={TemplateBinding IsDropDownOpen}
AllowsTransparency =真调焦=FALSEPopupAnimation =幻灯片>
<电网NAME =下拉SnapsToDevicePixels =真
了minWidth ={TemplateBinding ActualWidth的}了maxHeight ={TemplateBinding MaxDropDownHeight}>
< BORDER X:NAME =DropDownBorder背景=的WhiteSmoke
了borderThickness =1BorderBrush =黑/>
<列表框名称=DropDownContent风格={StaticResource的单选按钮列表}
WIDTH ={TemplateBinding了maxWidth}
的SelectedValue ={绑定路径=的SelectedValue,的RelativeSource = {的RelativeSource TemplatedParent}}
的ItemsSource ={TemplateBinding的ItemsSource}>
< /列表框>
< /网格和GT;
< /弹出>
< /网格和GT;
< /控件模板>
< /Setter.Value>
< /二传手>
< Style.Triggers>
< /Style.Triggers>
< /样式和GT;
我的是使用看起来像这样:
<组合框名称=SourceComboCtl
文本=源
SelectedValuePath =codeID
的DisplayMemberPath =姓名
的SelectedValue ={绑定路径=来源$ C $ C}
风格={StaticResource的RadioButtonCombo}
WIDTH =60
了maxWidth =150/>
我有点困惑,为什么你会先覆盖一个列表框来显示单选按钮,然后覆盖一个组合框使用列表框来显示单选按钮
为什么不覆盖 ComboBox.ItemTemplate
和产品总数跳过列表框?组合框具有的SelectedItem /值过
这是我的风格RadioButtonListBox。我只是改变了,它说的ListBox
说组合框
<风格X:键=RadioButtonComboBoxStyle的TargetType ={X:类型组合框}>
< setter属性=BorderBrushVALUE =透明/>
< setter属性=KeyboardNavigation.DirectionalNavigationVALUE =周期/>
< setter属性=ItemContainerStyle>
< Setter.Value>
<风格的TargetType ={X:类型ComboBoxItem}>
< setter属性=保证金VALUE =2,2,2,0/>
< setter属性=模板>
< Setter.Value>
<&控件模板GT;
< BORDER =背景透明>
<单选IsHitTestVisible =FALSE调焦=假
CONTENT ={TemplateBinding内容presenter.Content}
器isChecked ={绑定路径= IsSelected,的RelativeSource = {的RelativeSource TemplatedParent},模式=双向}/>
< /边框>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /Setter.Value>
< /二传手>
< /样式和GT;
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" />
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>
这篇关于在组合框弹出绑定单选按钮,列表框将selectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!