扩展一个 Expander 应该也扩展所有其他 Expander

Expanding one Expander should expand all other Expanders as well

我在 WPF 应用程序的一个页面上有多个扩展器。我想将一个扩展器触发的操作重复给其他扩展器。即在一个扩展器的扩展事件中,所有其他扩展器也应该扩展。同样会崩溃。任何代码片段的想法将不胜感激。

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Expander Grid.Row="0" IsExpanded="True">
        <Expander.Header>
            Expander 1
        </Expander.Header>
        <Expander.Content>
            This is first Expander
        </Expander.Content>
    </Expander>

    <Expander Grid.Row="1" IsExpanded="True">
        <Expander.Header>
           Expander 2
        </Expander.Header>
        <Expander.Content>
            This is Second Expander
        </Expander.Content>
    </Expander>

</Grid>

这可以通过多种方式来解决。最好的方法是在 viewmodel 中设置一个 属性 并且 link 所有扩展器到 属性。

检查以下代码:

  1. 向视图模型添加了 属性 并引发了 Inotify属性已更改
  2. 将其绑定到 isexpanded 如下。

IsExpanded="{Binding ExpandAll, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"

完整代码如下:

MainWindow.xaml

<Window x:Class="SOF.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SOF"
        mc:Ignorable="d"
        Title="Window2" Height="450" Width="800">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
        <Expander Grid.Row="0" IsExpanded="{Binding ExpandAll, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" >
                <Expander.Header>
                    Expander 1
                </Expander.Header>
                <Expander.Content>
                    This is first Expander
                </Expander.Content>
            </Expander>

        <Expander  Grid.Row="1" IsExpanded="{Binding ExpandAll, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}">
                <Expander.Header>
                    Expander 2
                </Expander.Header>
                <Expander.Content>
                    This is Second Expander
                </Expander.Content>
            </Expander>

        </Grid>
    </Window>

MainWindow.cs

   public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
            ExpanderViewModel evm = new ExpanderViewModel();
            this.DataContext = evm;
        }
    }
}

ViewModel.cs

 public abstract class ObservableModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propname = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
        }
        public ObservableModel() { }
    }

    public class ExpanderViewModel: ObservableModel
    {
        private bool mExpandAll;
        public bool ExpandAll
        {
            get { return mExpandAll; }
            set { mExpandAll = value; OnPropertyChanged(); }
        }

        public ExpanderViewModel() { }
    }