这段代码是用于做导航选项的,项目中暂时不用放在这里以备以后查阅。 ConfigUIManage _uiManage = new ConfigUIManage(); IList<Button> btnlist = new List<Button>(); Button currentBtn = null; /// <summary> /// 初始化,控制栏 /// </summary> private void InitControl() { btnlist.Add(CreateItem( "播放方案管理", new object[][] { new object[] {"序列方案", "Resources/apply.png",ConfigUIType.SequenceUI }, new object[] {"本地方案", "Resources/forward.png",ConfigUIType.LocalUI }, new object[] {"电视墙方案", "Resources/startup_items_disabled_1.png",ConfigUIType.TvWallUI } } )); btnlist.Add(CreateItem( "开关控制", new object[][] { new object[]{"分组控制", "Resources/work_group1.png",ConfigUIType.GroupCtrl}, new object[]{"独立控制", "Resources/my_computer.png",ConfigUIType.SingleCtrl} } )); btnlist.Add(CreateItem( "自定义设置", new object[][] { new object[]{"本地设置", "Resources/apply.png",ConfigUIType.LocalSetting}, new object[]{"网络设置", "Resources/startup_items_disabled_1.png",ConfigUIType.NetSetting}, new object[]{"自定义分组", "Resources/24_custom.png",ConfigUIType.CustomGroup} } )); btnlist.Add(CreateItem( "报警管理", new object[][] { new object[]{"报警配置", "Resources/AlarmSetting.png",ConfigUIType.AlarmSetting} } )); btnlist.Add(CreateItem( "锁屏管理", new object[][] { new object[]{"锁屏配置", "Resources/AlarmSetting.png",ConfigUIType.LockScreen} } )); //设置选中项 this.InitDock(0); } /// <summary> /// 创建大项 /// </summary> /// <param name="items">小项集合</param> /// <param name="ItemHeader">大项标题</param> /// <returns>创建结果</returns> private Button CreateItem(string ItemHeader, object[][] items) { ListBox lstbox = new ListBox(); lstbox.Style = (Style)Application.Current.TryFindResource("SettingListBox"); lstbox.Margin = new Thickness(1, 1, 0, 2); foreach (object[] item in items) { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(item[1] as string, UriKind.Relative); bitmap.EndInit(); Image img = new Image(); img.Width = 20; img.Height = 20; img.Source = bitmap; CustomListBoxItem lstboxitem = new CustomListBoxItem(); lstboxitem.Selected += new RoutedEventHandler(lstboxitem_Selected); lstboxitem.DataContext = item[2]; lstboxitem.Caption = item[0] as string; lstboxitem.Content = img; lstboxitem.Style = (Style)Application.Current.TryFindResource("SettingListBoxItem"); lstbox.Items.Add(lstboxitem); } TextBlock txtblock = new TextBlock(); txtblock.FontSize = 12; txtblock.FontFamily = new System.Windows.Media.FontFamily("宋体"); txtblock.Text = ItemHeader; txtblock.FontWeight = FontWeights.Bold; Button btn = new Button(); btn.Height = 30; btn.Content = txtblock; btn.HorizontalContentAlignment = HorizontalAlignment.Left; btn.HorizontalAlignment = HorizontalAlignment.Stretch; btn.Padding = new Thickness(16, 1, 1, 1); btn.Click += this.Button_Click; btn.Tag = lstbox; btn.Style = (Style)Application.Current.TryFindResource("ItemButton"); btn.Cursor = Cursors.Hand; return btn; } /// <summary> /// 显示当前项 /// </summary> /// <param name="index">当前项索引</param> private void InitDock(int index) { myDock.Children.Clear(); for (int i = 0; i <= index; i++) { myDock.Children.Add(btnlist[i]); DockPanel.SetDock(btnlist[i], Dock.Top); } for (int j = btnlist.Count - 1; j > index; j--) { myDock.Children.Add(btnlist[j]); DockPanel.SetDock(btnlist[j], Dock.Bottom); } myDock.Children.Add(btnlist[index].Tag as UIElement); this.currentBtn = btnlist[index]; //选中一个大项时默认选中第一个小项 ListBox lstbox = (btnlist[index].Tag as ListBox); CustomListBoxItem tmp = null; if (lstbox != null && lstbox.Items.Count > 0 && (tmp = (lstbox.Items[0] as CustomListBoxItem)) != null) { tmp.IsSelected = false; tmp.IsSelected = true; } } //选择大项 private void Button_Click(object sender, RoutedEventArgs e) { Button btn = e.Source as Button; if (currentBtn == null || currentBtn != btn) { int index = btnlist.IndexOf(btn); this.InitDock(index); } } //选择小项 void lstboxitem_Selected(object sender, RoutedEventArgs e) { var item = e.Source as CustomListBoxItem; ConfigUIType uitype = (ConfigUIType)item.DataContext; switch (uitype) { case ConfigUIType.SequenceUI: this.border.Child = _uiManage.SequenceUI; break; case ConfigUIType.LocalUI: this.border.Child = _uiManage.LocalUI; break; case ConfigUIType.TvWallUI: this.border.Child = _uiManage.TvWallUI; break; case ConfigUIType.LocalSetting: this.border.Child = _uiManage.LocalSetting; break; case ConfigUIType.NetSetting: this.border.Child = _uiManage.NetSetting; break; case ConfigUIType.CustomGroup: this.border.Child = _uiManage.CustomGroup; break; case ConfigUIType.SingleCtrl: this.border.Child = _uiManage.SingleCtrl; break; case ConfigUIType.GroupCtrl: this.border.Child = _uiManage.GroupCtrl; break; case ConfigUIType.AlarmSetting: this.border.Child = _uiManage.AlarmSetting; break; case ConfigUIType.LockScreen: this.border.Child = _uiManage.LScreen; break; } }