WPF扩展器-每次只能扩展一个扩展器

本文关键字:扩展器 一个 扩展 WPF | 更新日期: 2023-09-27 18:17:35

在WPF列表视图中,我创建了几个WPF展开器,在每个展开器中我有几个项目。现在我想每次只展开其中一个展开器…也就是说,如果其中一个膨胀,所有其他膨胀器都会被压缩。你能帮帮我吗?请注意,扩展器是由ItemTemplate创建的!

XMAL:

<UserControl x:Class="DataRetrieval.Views.ParametersView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                  xmlns:local="clr-namespace:DataRetrieval.Views"
                  d:DesignWidth="400"  Height=" 600" Background="White">
            <UserControl.Resources>
                <DataTemplate x:Key="StringDataTemplate">
                    <StackPanel Margin="5" >
                            <TextBlock Text="{Binding Name}" />
                        <TextBox Text="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged}" Width="290" />
                    </StackPanel>
                </DataTemplate>
                <DataTemplate x:Key="DateDataTemplate">
                    <StackPanel Margin="5" >
                            <ComboBox  SelectedIndex="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged}" Text="{Binding Name}"  Width="290" ItemsSource="{Binding UserItems}"/>
                        <DatePicker SelectedDate="{Binding multitype.datetime}" Width="200"/>
                    </StackPanel>
                </DataTemplate>

                <local:ParamControlTemplateSelector x:Key="myParamTemplateSelector" />
            </UserControl.Resources>
        <Border BorderBrush="LightGray" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8" Margin="20"  Background="#FFF3F3F3">
            <Grid Margin="5">
                <ScrollViewer VerticalScrollBarVisibility="Auto">
                    <ListView x:Name="myListView1" ItemsSource="{Binding Qtables,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="{Binding SelectedIndex}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Expander Header="{Binding Name}"  Width="320" IsExpanded="{Binding IsExpanded}" >
                                        <StackPanel   Height="600">
                                            <TextBlock Text="{Binding Name}" />
                                            <ListView x:Name="myListView" ItemsSource="{Binding Params,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplateSelector="{StaticResource myParamTemplateSelector}" SelectedIndex="{Binding CurrentUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                            </ListView>
                                        </StackPanel>
                                    </Expander>
                            </DataTemplate>
                    </ListView.ItemTemplate>
                   </ListView>
                </ScrollViewer>
            </Grid>
        </Border>
    </UserControl>

视图模型

namespace DataRetrieval.ViewModel
{
    class ParametersViewModel:BindableBase
    {
        public ParametersViewModel()
        {
            AParams = new WhereParams();
            Qtables = new QDatatables();
            for (int j = 0; j < 5;j++ )
            {

                for (int i = 0; i < 10; ++i)
                {
                    int[] numbers;
                    numbers = new int[3] { 1, 2, 3 };
                    var parameter = new WhereParam { ID = i, Name = "Name " + i.ToString() };
                    if (i == 2 || i == 4)
                    {
                        parameter.Type = ParamTypeEnum.datetimeType;
                    }
                    AParams.Add(parameter);
                }
                var qtable = new QDatatable {  ID=j, Name = j.ToString() + "QTable",  Params = AParams};
                Qtables.Add(qtable);
            }
        }
        private WhereParam _parameter;
        public WhereParam Parameter
        {
            get { return _parameter; }
            set
            {
                SetProperty(ref _parameter, value);
                }
        }
        private WhereParams _paramas;
        public WhereParams AParams
        {
            get { return _paramas; }
            set { SetProperty(ref _paramas, value); }
        }
        private QDatatables _qtables;
        public QDatatables Qtables
        {
            get { return _qtables; }
            set
            {
                SetProperty(ref _qtables, value);
            }
        }


    }
}

WPF扩展器-每次只能扩展一个扩展器

这可以通过修改Model来处理IsExpanded

来实现

Model for QTables:

 public class QDatatables : BindableBase
    {
        public QDatatables()
        {
            List.CollectionChanged += List_CollectionChanged;
        }
        void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {            
            foreach (var item in e.NewItems)
            {
                var test = item as QDatatable;
                test.PropertyChanged += test_PropertyChanged;
            }            
        }
        void test_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsExpanded")
            {
                var cur = sender as QDatatable;
                if (cur.IsExpanded == true)
                {
                    foreach (var item in List)
                    {
                        if (item.Name != cur.Name && item.IsExpanded == true)
                        {
                            item.IsExpanded = false;
                        }
                    }    
                }
            }
        }
        private ObservableCollection<QDatatable> _list;
        public ObservableCollection<QDatatable> List
        {
            get { return _list ?? (_list=new ObservableCollection<QDatatable>()); }
            set { SetProperty(ref _list, value); }
        }

    }