Reputation: 903
I have multiple Expanders on a page of WPF application. I want to repeat the action triggered for one expander to others. i.e. on expand event of one expander, all other expanders should expand as well. Same will go for collapse. Any idea of code snippet would be much appreciated.
<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>
Upvotes: 0
Views: 44
Reputation: 602
This can be approached by several ways. Best way is to setup a property inside the viewmodel and link all your expanders to that property.
Check codes below:
IsExpanded="{Binding ExpandAll, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
Complete codes below:
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() { }
}
Upvotes: 1