<Window x:Class="exSplitter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!--GridSplitter样式-->
<Style x:Key="GridSplitterPreviewStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Fill="#80000000"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="GridSplitterStyle1" TargetType="{x:Type GridSplitter}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="PreviewStyle" Value="{StaticResource GridSplitterPreviewStyle}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridSplitter}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<Button Name="btnExpend" Content=":" VerticalAlignment="Center" Height="50" Panel.ZIndex="0" Cursor="Hand"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Name="grdWorkbench">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.282*"/>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="0.718*"/>
</Grid.ColumnDefinitions>
<Grid Name="grd1" Margin="0" Background="#FFD8D8D8"/>
<GridSplitter x:Name="gsSplitterr" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0" Background="#FF9F9F9F" Style="{DynamicResource GridSplitterStyle1}"/>
<Grid Name="grd2" Grid.Column="2" Margin="5,0,-5,0" Background="#FFEBF5F7"/>
</Grid>
</Window>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace exSplitter
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
GridLength m_WidthCache;
public MainWindow()
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
void MainWindow_Loaded(object sender, RoutedEventArgs e)
//获取GridSplitterr的cotrolTemplate中的按钮btn,必须在Loaded之后才能获取到
Button btnGrdSplitter = gsSplitterr.Template.FindName("btnExpend", gsSplitterr) as Button;
if (btnGrdSplitter != null)
btnGrdSplitter.Click += new RoutedEventHandler(btnGrdSplitter_Click);
void btnGrdSplitter_Click(object sender, RoutedEventArgs e)
GridLength temp = grdWorkbench.ColumnDefinitions[0].Width;
GridLength def = new GridLength();
if (temp.Equals(def))
//恢复
grdWorkbench.ColumnDefinitions[0].Width = m_WidthCache;
//折叠
m_WidthCache = grdWorkbench.ColumnDefinitions[0].Width;
grdWorkbench.ColumnDefinitions[0].Width = def;
代码:http://files.cnblogs.com/kooleven/exSplitter.rar
转载于:https://www.cnblogs.com/kooleven/archive/2013/04/21/3034395.html
文笔不好,将就看代码吧,非常简单的一个实现,左右两个Grid加了颜色为了更方便看到效果。<Window x:Class="exSplitter.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://sc...
Expander控件有一个箭头按钮。单击箭头时,Expander中的子元素将显示或隐藏。箭头“展开”控件,使其子控件可见。
接下来,我们可以在扩展器中添加元素——这些元素将被扩展器箭头隐藏或显示。在这里,我向扩展器添加了一个 ImageRadioButton 按钮。
注意我修改了 Expander 元素的“Header”属性。单击此处的扩展器会显示说明。
IsExpanded我将 IsExpanded 属性修改为 false。这意味着当程序启动时,描述是不可见的。
TextBlock在 TextBl
界面样式选用了Materail DesginUI ,十分好用,外观漂亮,开发常规需求,足够使用了。
Xmal中的布局,其中有一列"menuLeft" 双向绑定了ViewModel里的MenuWidth属性, GridSplitter左右拖拽时动态改变了MenuWidth属性值;
<Grid Grid.Row="2">
WPF Combobox是一个常见的下拉列表控件,它能够快速地展示和选择不同的选项。在某些情况下,我们需要实现 Combobox 的多选功能,以便用户能够选择多个选项。本文将为您详细介绍如何使用 WPF Combobox 实现多选。
实现多选的一种简单的方式是将 WPF Combobox 显示为列表框(ListBox),然后允许用户在列表中选择多个选项。要实现这样的多选功能,需要使用 ItemsControl 和 ListBox 控件。首先,使用ItemsControl将要用于展示的选项集合附加到 Combobox 上。
```xaml
<!--定义ItemsControl-->
<ItemsControl ItemsSource="{Binding Selection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="5" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
其中,`Selection`是选项的集合。接下来,将 Combobox 的模板更改为 ListBox,使其在下拉列表中显示所有选项。
```xaml
<!--定义Combobox-->
<ComboBox x:Name="ComboBox">
<ComboBox.Template>
<ControlTemplate TargetType="ComboBox">
<ToggleButton x:Name="ToggleButton" Content="Click" />
<Popup x:Name="Popup">
<ListBox ItemsSource="{Binding Selection}" SelectionMode="Multiple" />
</Popup>
</Grid>
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
在 Combobox 的模板中,使用 ToggleButton 将 Popup 隐藏和显示。Popup 中包含 ListBox,且设置 SelectionMode 属性为 Multiple,使其能够选择多个选项。此时,每个选项在 Popup 中显示为 ListBox 中的一个项,而不是 Combobox 的下拉列表中的一个项。
在编写代码时,需要先将 Combobox 显示为 ListBox,然后使用 Popup 特性将其隐藏。此外,还需要为 ListBox 添加事件处理程序,以便在用户选择选项时更新组合框的选项集合。
```c#
//实现多选
ComboBox.IsEditable = true;
var itemsControl = ((ComboBox)ComboBox).Template.FindName("PART_ItemList", ComboBox) as ItemsControl;
var listBox = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ListBox;
if (listBox != null)
listBox.SelectionMode = SelectionMode.Multiple;
listBox.SelectionChanged += listBox_SelectionChanged;
//更新选项
var listBox = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ListBox;
if (listBox != null)
listBox.SelectionChanged -= listBox_SelectionChanged;
for (int i = 0; i < listBox.SelectedItems.Count; ++i)
Selection.Add(listBox.SelectedItems[i].ToString());
listBox.SelectionChanged += listBox_SelectionChanged;
最后,用户选择多个选项时,会在 Selection 集合中添加选项,当用户关闭 Popup 时,将更新选项的 Text 和 Popup 的 IsOpen 属性。
如上所述,这是使用 WPF Combobox 实现多选的一种简单方法。我们先将 Combobox 显示为 ListBox,再使用 Popup 特性将Popup隐藏,然后添加事件处理程序以更新选项集合。这种方式虽然看起来有点繁琐,但要实现多选需要的代码量很少,而且在使用上比一些自定义控件的方式更灵活简便。