8,735
社区成员
发帖
与我相关
我的任务
分享
<UserControl
x:Class="SLApp.MultipleExpander"
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:sysData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
mc:Ignorable="d"
xmlns:local="clr-namespace:SLApp"
d:DesignWidth="640" d:DesignHeight="480" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
Loaded="UserControl_Loaded"
>
<Grid x:Name="LayoutRoot">
<StackPanel Name="StackPanel1">
<StackPanel.Resources>
<local:ExpanderToBooleanConverter x:Key="ExpanderToBooleanConverter" />
</StackPanel.Resources>
<toolkit:Expander Header="Expander 1"
IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=1}">
<TextBlock>Expander 1</TextBlock>
</toolkit:Expander>
<toolkit:Expander Header="Expander 2"
IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=2}">
<TextBlock>Expander 2</TextBlock>
</toolkit:Expander>
<toolkit:Expander Header="Expander 3"
IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=3}">
<TextBlock>Expander 3</TextBlock>
</toolkit:Expander>
<toolkit:Expander Header="Expander 4"
IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=4}">
<TextBlock>Expander 4</TextBlock>
</toolkit:Expander>
</StackPanel>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Data;
using System.Globalization;
namespace SLApp
{
public partial class MultipleExpander : UserControl
{
public MultipleExpander()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
StackPanel1.DataContext = new ExpanderListViewModel();
}
}
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;
}
}
public class ExpanderListViewModel
{
public Object SelectedExpander { get; set; }
}
}
public class ExpanderListViewModel : INotifyPropertyChanged
{
private object selectedExpander = 1;
public object SelectedExpander
{
get { return this.selectedExpander; }
set
{
if (this.selectedExpander != value)
{
this.selectedExpander = value;
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SelectedExpander"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class ExpanderToBoolConverter: 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;
}
}
public class ExpanderListViewModel
{
public object SelectedExpander
{
set;
get;
}
}
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test.Converters"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ExpanderToBoolConverter x:Key="ExpanderToBoolConverter"></local:ExpanderToBoolConverter>
</Window.Resources>
<Grid>
<StackPanel x:Name="myStackPanel" Orientation="Horizontal">
<Expander Header="Expander 1"
IsExpanded="{Binding Path=SelectedExpander,Mode=TwoWay, Converter={StaticResource ResourceKey=ExpanderToBoolConverter}, ConverterParameter=1}">
<TextBlock>Expander 1</TextBlock>
</Expander>
<Expander Header="Expander 2"
IsExpanded="{Binding Path=SelectedExpander,Mode=TwoWay,Converter={StaticResource ResourceKey=ExpanderToBoolConverter}, ConverterParameter=2}">
<TextBlock>Expander 2</TextBlock>
</Expander>
<Expander Header="Expander 3"
IsExpanded="{Binding Path=SelectedExpander,Mode=TwoWay,Converter={StaticResource ResourceKey=ExpanderToBoolConverter}, ConverterParameter=3}">
<TextBlock>Expander 3</TextBlock>
</Expander>
<Expander Header="Expander 4"
IsExpanded="{Binding Path=SelectedExpander,Mode=TwoWay,Converter={StaticResource ResourceKey=ExpanderToBoolConverter}, ConverterParameter=4}">
<TextBlock>Expander 4</TextBlock>
</Expander>
</StackPanel>
</Grid>
</Window>
public MainWindow()
{
InitializeComponent();
this.myStackPanel.DataContext = new ExpanderListViewModel();
}