silverlight 多个Expander 联动,只能有一个Expander是打开的

duoshanx 2012-04-26 07:41:12
我想做成横排的4个Expander,类似于TabControl的效果,就是以能有一个打开。

参照下面的URL,做了一下,但是没有实现。
1,它是竖着放的。
2,能同时打开多个。
http://stackoverflow.com/questions/4449000/wpf-multiple-expander-have-to-collapse-if-one-is-expanded

下面是我的代码。


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

...全文
639 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
forever772 2014-09-21
  • 打赏
  • 举报
回复 2
Header放入Radiobutton,Expander的IsExpanded绑定Radiobutton的IsChecked属性。
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

silverlight中对象属性变化时不会自动通知UI对象,需要实现INotifyPropertyChanged接口,如下。
C# code

public class ExpanderListViewModel : INotifyPropertyChanged
{
private object selectedExpander = 1;
publi……
[/Quote]

用6楼的方法就可以实现楼主的要求。
但有个问题问一下sundayX,
silverlight中对象属性变化时不会自动通知UI对象,需要实现INotifyPropertyChanged接口,

这个对于wpf也不会通知的,也要继承INotifyPropertyChanged接口。但这个例子中,对于wpf,这个类不继承这个接口确实也能实现楼主的要求,感觉很奇怪。
能解释一下吗?或者有人知道的说一下,一下子有点不明白了。
sundayX 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
这个对于wpf也不会通知的,也要继承INotifyPropertyChanged接口。但这个例子中,对于wpf,这个类不继承这个接口确实也能实现楼主的要求,感觉很奇怪。
[/Quote]
这个还真第一次遇到呢,具体原因我也不清楚。
感觉在执行IValueConverter的ConvertBack时,wpf能够自动的去通知界面更新而不依靠自定义属性的通知,但silverlight不行,silverlight需要依靠自定义属性的通知。
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
引用 7 楼 的回复:
这个对于wpf也不会通知的,也要继承INotifyPropertyChanged接口。但这个例子中,对于wpf,这个类不继承这个接口确实也能实现楼主的要求,感觉很奇怪。

这个还真第一次遇到呢,具体原因我也不清楚。
感觉在执行IValueConverter的ConvertBack时,wpf能够自动的去通知界面更新而不依靠自定义属性的通知,但silverlight不行……
[/Quote]

我和你的想法一样,我也觉得问题出现在Converter上,再研究下。
sundayX 2012-04-27
  • 打赏
  • 举报
回复
silverlight中对象属性变化时不会自动通知UI对象,需要实现INotifyPropertyChanged接口,如下。

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;
}
assky124 2012-04-27
  • 打赏
  • 举报
回复
ControlToolkit 里不是有 Accordion控件么
  • 打赏
  • 举报
回复
哦,我看你发的链接是wpf, 所以用了wpf.

回头我再用silverlight看看,现在没环境。
duoshanx 2012-04-27
  • 打赏
  • 举报
回复
谢谢,在普通应用程序里,确实能实现。
但是相同的代码,在 silverlight 里却实现不了。。。。有什么差别吗?
  • 打赏
  • 举报
回复
Converter类:
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;
}

}


ViewModel类:
public class ExpanderListViewModel
{
public object SelectedExpander
{
set;
get;
}
}


可以正常运行。
  • 打赏
  • 举报
回复
设计页面MainWindow.xaml
<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>


codebehind file: MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
this.myStackPanel.DataContext = new ExpanderListViewModel();
}

案例中的控件全部有源代码示范学习为主。 1.Calendar组件(日历组件) 2.ChildWindow组件(子窗体组件) 3.浮动窗体组件 4.GridSplitter组件 5.TabControl组件 6-7-8.TreeView组件 9.DtarGrid 10.强大的DataGrid组件[2]_数据交互之ADO.NET Entity Framework 11.强大的DataGrid组件[3]_数据交互之Linq to SQL——Silverlight学习笔记 12-13.强大的DataGrid组件[4]_实现CURD 14.强大的DataGrid组件[6]_调用存储过程服务端分页 15.强大的DataGrid组件[7]_自定义DataGrid 16.强大的DataGrid组件[8]_内嵌ComboBox动态数据联动 17.强大的DataGrid组件[9]_自定义头模板(HeaderTemplate) 18.强大的DataGrid组件[10]_自定义脚模板(FooterTemplate) 19.强大的DataGrid组件[11]_主从(Master-Details)的实现 20.强大的DataGrid组件[12]_分组(Group) 21.强大的DataGrid组件[13]_字段过滤(Filter) 22.强大的DataGrid组件[14][Final]_数据验证 23.有关DataForm组件的研究_基础知识和实现服务端批量CURD 24.有关DataForm组件的研究_显示多重数据模型集合 25.有关DataForm组件的研究_自定义DataForm模板 26.有关Data Input类组件的研究 27.有关Accordion组件的研究 摘要: Accordion组件在开发中常用于信息的分类显示,用于显示数据验证信息不错 28.TransitioningContentControl组件: TransitioningContentControl控件主要应用于变化内容的过渡呈现效果 29.有关Navigation的: 在Silverlight的程序设计中经常需要在多个XAML页面之间进行切换,以进行不同的功能操作 30.有关ImplicitStyleManager组件: ImplicitStyleManager组件的作用是封装一个附加的行为,该行为将一个框架元素内的相关资源词典内的样式传播至它的子元素。该组件同样提供了附加属性,使资源字典能从外部源加载。层次状样式同样被支持,这与WPF相类似 31.有关Theme(主题): 在Silverlight的开发中,为组件设置统一的主题会让程序的外观显得美观大方 32.有关Expander组件的研究 摘要: Expander组件常用做边栏目录的分类,比如Windows中“我的文档”的侧边栏。本文将为大家介绍该组件的基本特性以及实际应用 33.有关ViewBox组件 摘要: ViewBox的作用是拉伸或延展位于其中的组件,使之有更好的布局及视觉效 34.有关WrapPanel组件 摘要: WrapPanel组件作用是从左至右或从上至下依次安排位于其中的元素的位置,当元素超过该组件边缘时,它们将会被自动安排至下一行或列。该组件一般用于文本布局、拾色器、图片选择等。本文将为大家介绍该组件的基本特性以及应用实例 35.有关AutoCompleteBox组件 摘要: AutoCompleteBox(自动完成框)组件能加快我们的输入效率,同时也能够提高输入的联想效果 37.有关AutoCompleteBox组件的研究[2]_常用特性实例介绍 38.有关AutoCompleteBox组件的研究[3]_FilterMode和ItemFilter 摘要: 对于AutoCompleteBox组件而言,设置合理的过滤模式有利于对数据的精确筛选 39.有关AutoCompleteBox组件的研究[4]_下拉框内嵌DataGrid与被嵌入DataGrid 摘要: 在AutoCompleteBox组件下拉框中嵌入DataGrid可以让我们更好地组织候选数据以达到更好的显示效果。与此类似的,在DataGrid组件中嵌入AutoCompleteBox组件可以便于我们进行数据的输入。本文将为大家讲述如何实现这两种效果 40.有关AutoCompleteBox组件的研究[5][Final]_集成搜索引擎搜索建议(Search Suggestion) 摘要: 在AutoCompleteBox组件中集成搜索引擎的功能是十分常见的,这有助于我们更好地与Web进行交互。本文将为大家讲述如何在在AutoCompleteBox组件中集成搜索引擎的搜索建议 41.有关Rating组件的研究 摘要: 我们经常能在网上发现为新闻、博客文章、图片或是电影视频的评分功能。在Silverlight中,使用Rating组件便能助我们完成以上的功能。本文将为大家介绍该组件的基础知识以及自定义应用方面等方面的内容 42.有关Input类组件的研究 摘要: Input输入类控件丰富了我们的输入形式,合理地运用之,可以加快我们录入的速度。本文将为大家介绍Input类组件中的其他4个组件ButtonSpinner、DomainUpDown、NumericUpDown以及TimePicker的基础知识及其简单运用 43.有关DataVisualization类组件的研究 摘要: Data Visualization类组件以直观的图表方式显示数据的分布,能够让我们更好地分析各数据的内在联系。本文主要向大家介绍该类组件的基本特性以及使用实例

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧