1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| <Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="MyData"
XPath="/Info">
<x:XData>
<Info xmlns="">
<Item Name="Item 1"
Category="Cat1" />
<Item Name="Item 2"
Category="Cat1" />
<Item Name="Item 3"
Category="Cat2" />
<Item Name="Item 4"
Category="Cat2" />
<Item Name="Item 5"
Category="Cat2" />
<Item Name="Item 6"
Category="Cat3" />
</Info>
</x:XData>
</XmlDataProvider>
<CollectionViewSource x:Key='src'
Source="{Binding Source={StaticResource MyData}, XPath=Item}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Category" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<CollectionViewSource x:Key='src2'
Source="{Binding Source={StaticResource MyData}, XPath=Item}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Category" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<ControlTemplate x:Key="CBoxTemplate"
TargetType="{x:Type ComboBox}">
<Grid SnapsToDevicePixels="true"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}">
<Border x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="1">
<Grid Grid.IsSharedSizeScope="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="ComboBoxButton" />
</Grid.ColumnDefinitions>
<Border Margin="{TemplateBinding Padding}"
x:Name="SelectedItemBorder"
Grid.ColumnSpan="2" />
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Grid.Column="1"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" />
<ToggleButton Background="Transparent"
Grid.ColumnSpan="3"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
</Border>
<Popup x:Name="PART_Popup"
Focusable="false"
AllowsTransparency="true"
IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
Placement="Bottom">
<ListView ItemsSource="{TemplateBinding ItemsSource}"
SelectedValuePath="{TemplateBinding SelectedValuePath}"
SelectedValue="{TemplateBinding SelectedValue}"
IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}">
</ListView>
</Popup>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="CBoxTemplate2"
TargetType="{x:Type ComboBox}">
<Grid SnapsToDevicePixels="true"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}">
<Border x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="1">
<Grid Grid.IsSharedSizeScope="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="ComboBoxButton" />
</Grid.ColumnDefinitions>
<Border Margin="{TemplateBinding Padding}"
x:Name="SelectedItemBorder"
Grid.ColumnSpan="2" />
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Grid.Column="1"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" />
<ToggleButton Background="Transparent" ClickMode="Press" Focusable="False"
Grid.ColumnSpan="3"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
</Border>
<Popup x:Name="PART_Popup"
Focusable="false"
AllowsTransparency="true"
IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
Placement="Bottom">
<Border x:Name="DropDownBorder"
BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}"
BorderThickness="1"
CornerRadius="5,5,5,5">
<ScrollViewer MaxHeight="100"
VerticalScrollBarVisibility="Auto"
Background="White">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Page.Resources>
<StackPanel Width="200">
<ComboBox ItemsSource='{Binding Source={StaticResource src}}'
DisplayMemberPath="@Name"
BorderThickness="1"
Template="{StaticResource CBoxTemplate}" />
<ComboBox ItemsSource='{Binding Source={StaticResource src2}}'
DisplayMemberPath="@Name"
BorderThickness="1"
Template="{StaticResource CBoxTemplate2}" />
</StackPanel>
</Page> |
Partager