绑定RadioButton IsChecked到ListBoxItem IsSelected和ListBox IsFocused [英] Binding RadioButton IsChecked to ListBoxItem IsSelected and ListBox IsFocused

查看:147
本文介绍了绑定RadioButton IsChecked到ListBoxItem IsSelected和ListBox IsFocused的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到其他类似的问题,但不知何故我仍然无法正常工作。这是情况。



我有我有一个 ListBox 我的观点模型。每个视图模型都有一个显示在另一个嵌套列表框中的子项列表。我正在使用一个 DataTemplate 来达到这个目的。

我要什么
我希望子项目有一个 RadioButton ,当选择 ListBoxItem 时选中,当 ListBox 具有焦点(内部 ListBox )。


目前,上面的 IsSelected 部分工作得很好,但是当我从一个视图模型移动到其他(即第一个ListBox丢失焦点)第一个 ListBox 上的单选按钮保持选中状态。



这是code:

 < 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 这部分工作很好,即列表中只有一个项目有随时选择的单选按钮。但是,当控件失去焦点时,所选项目的单选按钮仍然处于选中状态(不是我想要的)。

解决方案

这是xaml结束了工作。禁用单选按钮似乎是这里的关键。

 < ControlTemplate TargetType ={x:Type ListBoxItem}> 
<网格>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition />
< /Grid.ColumnDefinitions>
< RadioButton x:Name =rbIsSelectedIsChecked =FalseIsEnabled =FalseVerticalAlignment =CenterHorizo​​ntalAlignment =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>

查看默认模板帮助了很多。


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 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.

What I Want I want the children items to have a RadioButton that is selected when the ListBoxItem is selected and when the ListBox has focus (the inner ListBox).

Currently, the 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.

Here is the code:

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

I have also tried MultiBinding, but that wasnt working either. Any suggestions?

UPDATE Update to include my attempt at 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>

And the converter:

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();
    }
}

The 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).

解决方案

Here is the xaml that ended up working. Disabling the radio button seems to be the key here.

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

Looking at the default template helped alot.

这篇关于绑定RadioButton IsChecked到ListBoxItem IsSelected和ListBox IsFocused的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆