【问题标题】:Expanding one Expander should expand all other Expanders as well扩展一个扩展器也应该扩展所有其他扩展器
【发布时间】:2019-07-25 06:31:47
【问题描述】:

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

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

【问题讨论】:

    标签: .net wpf xaml wpf-controls


    【解决方案1】:

    这可以通过多种方式来解决。最好的方法是在视图模型中设置一个属性并将所有扩展器链接到该属性。

    检查以下代码:

    1. 向视图模型添加了一个属性并提高了 Inotifypropertychanged
    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() { }
        }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多