本文介绍了如果扩展一个,则多个扩展器必须崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有4个扩展器控件。扩展一个扩展器后,如何使所有其他扩展器折叠/关闭?

Having 4 Expander controls. When one expander is expanded how can I make all others collapse/close?

推荐答案

尝试以下代码:

        <StackPanel Name="StackPanel1">
            <StackPanel.Resources>
                <local:ExpanderToBooleanConverter x:Key="ExpanderToBooleanConverter" />
            </StackPanel.Resources>
            <Expander Header="Expander 1"
                      IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=1}">
                <TextBlock>Expander 1</TextBlock>
            </Expander>
            <Expander Header="Expander 2"
                      IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=2}">
                <TextBlock>Expander 2</TextBlock>
            </Expander>
            <Expander Header="Expander 3"
                      IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=3}">
                <TextBlock>Expander 3</TextBlock>
            </Expander>
            <Expander Header="Expander 4"
                      IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=4}">
                <TextBlock>Expander 4</TextBlock>
            </Expander>
        </StackPanel>



转换器:



Converter:

public class ExpanderToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (value == parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (System.Convert.ToBoolean(value)) return parameter;
        return null;
    }
}



ViewModel:



ViewModel:

public class ExpanderListViewModel
{
    public Object SelectedExpander { get; set; }
}



初始化



Initialization

StackPanel1.DataContext = new ExpanderListViewModel();






说明:



在XAML中,我们有4个扩展器。它们都从容器 StackPanel ViewModel (类型为 ExpanderListViewModel )。 c>通过 DataContext


Explanation:

In XAML we have 4 expanders. They all inherit a ViewModel (of type ExpanderListViewModel) from container StackPanel through DataContext.

它们都绑定到 ViewModel 类。并使用 ConverterParameter 绑定为自己定义了唯一索引。每当您展开扩展器时,该索引将保存在 SelectedExpander 属性中。并使用该索引,如果存储的索引与给定索引和匹配,转换器返回 true 如果存储的索引不匹配,则返回false

They all bind to single property on ViewModel class. And have defined a unique index for themselves using ConverterParameter in binding. That index gets saved in SelectedExpander property whenever you expand an expander. And using that index, the Converter returns true if the stored index matches with given index and false if stored index does not match.

Convert Converter 类的code> ConvertBack 方法,您将看到发生了什么。

Put a breakpoint in Convert and ConvertBack methods of Converter class and you will see what is going on.

这篇关于如果扩展一个,则多个扩展器必须崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 15:55