绑定RadioButton IsChecked到ListBoxItem IsSelected和ListBox IsFocused [英] Binding RadioButton IsChecked to ListBoxItem IsSelected and ListBox IsFocused
问题描述
我有我有一个 我要什么 目前,上面的 这是code: 我也试过 这是xaml结束了工作。禁用单选按钮似乎是这里的关键。 查看默认模板帮助了很多。 I've seen other questions very similar to this but somehow I still can't get it working. Here is the scenario. What I have
I have a What I Want
I want the children items to have a Currently, the Here is the code: I have also tried UPDATE
Update to include my attempt at And the converter: The Here is the xaml that ended up working. Disabling the radio button seems to be the key here. Looking at the default template helped alot. 这篇关于绑定RadioButton IsChecked到ListBoxItem IsSelected和ListBox IsFocused的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ListBox
我的观点模型。每个视图模型都有一个显示在另一个嵌套列表框中的子项列表。我正在使用一个 DataTemplate
来达到这个目的。
我希望子项目有一个 RadioButton
,当选择 ListBoxItem
时选中,当 ListBox
具有焦点(内部 ListBox
)。
IsSelected
部分工作得很好,但是当我从一个视图模型移动到其他(即第一个ListBox丢失焦点)第一个 ListBox
上的单选按钮保持选中状态。
< Style TargetType ={x:Type ListBox}>
< Setter Property =ItemContainerStyle>
< Setter.Value>
< Style TargetType ={x:Type ListBoxItem}>
< Setter Property =MarginValue =2/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type ListBoxItem}>
< RadioButton Focusable =False>
< RadioButton.Style>
< Style TargetType ={x:Type RadioButton}>
< Style.Triggers>
< DataTrigger Binding ={Binding Path = IsFocused,Mode = OneWay,RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type ListBox}}}Value =False>
< Setter Property =IsCheckedValue =False/>
< / DataTrigger>
< / style>
< RadioButton.IsChecked>
< Binding Path =IsSelectedMode =TwoWayRelativeSource ={RelativeSource TemplatedParent}/>
< ContentPresenter>< / ContentPresenter>
< / RadioButton>
< / ControlTemplate>
< / Setter>
< / style>
< / Setter>
< / style>
MultiBinding
也没有工作。有什么建议吗?
$ b $ p $ UPDATE
更新包括我的尝试 MultiBinding
:
< ControlTemplate TargetType ={x:Type ListBoxItem}>
< RadioButton>
< RadioButton.IsChecked>
< MultiBinding>
< MultiBinding.Converter>
< DataExportTool:AllTrueConverter />
< Binding Path =IsSelectedMode =TwoWayRelativeSource ={RelativeSource TemplatedParent}/>
< Binding Path =IsFocusedMode =OneWayRelativeSource ={RelativeSource TemplatedParent}/>
< / MultiBinding>
< ContentPresenter />
< / RadioButton>
< / ControlTemplate>
和转换器:
$ b $
public class AllTrueConverter:IMultiValueConverter
{
public object Convert(object [] values,Type targetType,object parameter,CultureInfo culture)
{
返回值.Cast< bool>()。All(x => x); (bool)
$ b $ public object [] ConvertBack(object value,Type [] targetTypes,object parameter,CultureInfo culture)
{
return Enumerable.Repeat值,2).Cast< object>()。ToArray();
$ / code> IsSelected
这部分工作很好,即列表中只有一个项目有随时选择的单选按钮。但是,当控件失去焦点时,所选项目的单选按钮仍然处于选中状态(不是我想要的)。
< ControlTemplate TargetType ={x:Type ListBoxItem}>
<网格>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition />
< /Grid.ColumnDefinitions>
< RadioButton x:Name =rbIsSelectedIsChecked =FalseIsEnabled =FalseVerticalAlignment =CenterHorizontalAlignment =CenterMargin =3/>
< ContentPresenter Grid.Column =1/>
< / Grid>
< ControlTemplate.Triggers>
< MultiTrigger>
< MultiTrigger.Conditions>
< Condition Property =IsSelectedValue =True/>
< Condition Property =Selector.IsSelectionActiveValue =True/>
< Setter Property =IsCheckedTargetName =rbIsSelectedValue =True/>
< / MultiTrigger>
< / ControlTemplate>
ListBox
that displays a list of my view models. Each view model has a list of children that are displayed in another, nested list box. I am using a DataTemplate
to achieve this.RadioButton
that is selected when the ListBoxItem
is selected and when the ListBox
has focus (the inner ListBox
). IsSelected
portion of the above is working great, however when I move from one view model to the other (i.e. the first ListBox loses focus) the radio button on the first ListBox
remains selected. <Style TargetType="{x:Type ListBox}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton Focusable="False">
<RadioButton.Style>
<Style TargetType="{x:Type RadioButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsFocused, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}}" Value="False">
<Setter Property="IsChecked" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
<RadioButton.IsChecked>
<Binding Path="IsSelected" Mode="TwoWay" RelativeSource="{RelativeSource TemplatedParent}" />
</RadioButton.IsChecked>
<ContentPresenter></ContentPresenter>
</RadioButton>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
MultiBinding
, but that wasnt working either. Any suggestions?MultiBinding
:<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton>
<RadioButton.IsChecked>
<MultiBinding>
<MultiBinding.Converter>
<DataExportTool:AllTrueConverter/>
</MultiBinding.Converter>
<Binding Path="IsSelected" Mode="TwoWay" RelativeSource="{RelativeSource TemplatedParent}"/>
<Binding Path="IsFocused" Mode="OneWay" RelativeSource="{RelativeSource TemplatedParent}"/>
</MultiBinding>
</RadioButton.IsChecked>
<ContentPresenter/>
</RadioButton>
</ControlTemplate>
public class AllTrueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.Cast<bool>().All(x => x);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return Enumerable.Repeat((bool)value, 2).Cast<object>().ToArray();
}
}
IsSelected
part of this works great, i.e. only one item in the list has the radiobutton selected at any time. However, when the control looses focus the selected item's radio button is still selected (not what I want).<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<RadioButton x:Name="rbIsSelected" IsChecked="False" IsEnabled="False" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3" />
<ContentPresenter Grid.Column="1"/>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True"/>
<Condition Property="Selector.IsSelectionActive" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="IsChecked" TargetName="rbIsSelected" Value="True"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>