在一个WPF ComboBox中使用三种不同的ComboBoxItem样式

WER*_*reo 3 c# wpf xaml combobox

因此,我正在尝试为正在处理的应用程序创建用户控件。基本上ToggleButtonComboBox。我能够ComboBox按照设计人员的要求在VS2015中模拟用户控件的一部分,但是我觉得我的想法并不是最好的方法。

首先,这是其外观截图的链接:https : //www.dropbox.com/s/019f4xqgu8r4i0e/DropDown.png

为此,我最终创建了3种不同的ComboBoxItem样式。第一个将a CheckBox,a TextBlock与the ContentPresenter和a 组合在一起Rectangle。第二个只有一个Separator,而最后一个只有TextBlock一个ContentPresenter。这是我的XAML,在本UserControl.Resources节中声明:

<Style x:Key="cbTestStyle" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Stretch"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBoxItem">
                <Border Name="Border"
                        Padding="5"
                        Margin="2"
                        BorderThickness="2"
                        CornerRadius="0"
                        BorderBrush="Transparent">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20"/>
                            <ColumnDefinition Width="75"/>
                            <ColumnDefinition Width="15"/>
                        </Grid.ColumnDefinitions>
                        <CheckBox Grid.Column="0"
                                  IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}"/>
                        <TextBlock Grid.Column="1"
                                   TextAlignment="Left"
                                   Foreground="Black">
                            <ContentPresenter/>
                        </TextBlock>
                        <Rectangle Grid.Column="2"
                                   Stroke="Black"
                                   Width="15"
                                   Height="15"
                                   Fill="{TemplateBinding Foreground}"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="True">
                        <Setter TargetName="Border" Property="BorderBrush" Value="Gray"/>
                        <Setter TargetName="Border" Property="Background" Value="LightGray"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="cbSeparatorStyle" TargetType="ComboBoxItem">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Separator/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="cbResetStyle" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBoxItem">
                <Border x:Name="Border"
                        Padding="5"
                        Margin="2"
                        BorderThickness="2"
                        CornerRadius="0"
                        BorderBrush="Transparent">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="1">
                            <ContentPresenter/>
                        </TextBlock>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="True">
                        <Setter TargetName="Border" Property="BorderBrush" Value="Gray"/>
                        <Setter TargetName="Border" Property="Background" Value="LightGray"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

我想我的第一个问题是,这是使我ComboBox看起来像所呈现的屏幕截图的最佳方法吗?

当然,我还需要解决更深层次的问题。首先,cbTestStyleComboBoxItem我希望能够动态填充。数据绑定是我显而易见的选择,但是最后使用分隔符和“ Reset”样式,我不确定如何做到这一点。我目前ComboBoxItem在XAML中具有s“硬编码”:

    <ComboBox x:Name="cbTestSelect"
              Height="34"
              Width="18" 
              IsEnabled="False">
        <ComboBoxItem Style="{StaticResource cbTestStyle}" Content="Test 1" Foreground="#7FFF0000" Selected="ComboBoxItem_Selected"/>
        <ComboBoxItem Style="{StaticResource cbTestStyle}" Content="Test 2" Foreground="#7F00FF00" Selected="ComboBoxItem_Selected"/>
        <ComboBoxItem Style="{StaticResource cbTestStyle}" Content="Test 3" Foreground="#7F0000FF" Selected="ComboBoxItem_Selected"/>
        <ComboBoxItem Style="{StaticResource cbSeparatorStyle}"/>
        <ComboBoxItem Style="{StaticResource cbResetStyle}" Content="Reset all"/>
    </ComboBox>
Run Code Online (Sandbox Code Playgroud)

在此示例中,理想情况下,我希望动态创建前三个项目,并使分隔符和“重置”项目保持静态。我对WPF还是比较陌生。我觉得想要在WinForms中创建此控件(该用户控件将在其中使用的应用程序)会复杂得多。另外,我还是试图引导我们更多地使用WPF。

任何帮助或其他问题或在线教程的链接将不胜感激。

Fed*_*gui 5

解决方案1:

使用a,CompositeCollection以便您可以使用DataBinding调出数据项,并使用常规XAML定义硬编码项:

<Window x:Class="WpfApplication31.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication31"
        Title="MainWindow" Height="350" Width="525"
        x:Name="view">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:DataItem}">
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsChecked}"/>
                <TextBlock Text="{Binding Text}"/>
                <Rectangle Stroke="Black" StrokeThickness="1"
                           Fill="{Binding Color}" Width="20"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <ComboBox VerticalAlignment="Center"
                  HorizontalAlignment="Center"
                  Width="100" x:Name="Combo">
            <ComboBox.Resources>
                <CompositeCollection x:Key="ItemsSource">
                    <CollectionContainer Collection="{Binding DataContext,Source={x:Reference view}}"/>
                    <Separator Height="10"/>
                    <Button Content="Clear All"/>
                </CompositeCollection>
            </ComboBox.Resources>

            <ComboBox.ItemsSource>
                <StaticResource ResourceKey="ItemsSource"/>
            </ComboBox.ItemsSource>
        </ComboBox>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

背后的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var colors = new[] {"Red", "Green", "Blue", "Brown", "Cyan", "Magenta"};

        this.DataContext =
            Enumerable.Range(0, 5)
                .Select(x => new DataItem
                {
                    Text = "Test" + x.ToString(),
                    Color = colors[x],
                    IsChecked = x%2 == 0
                });

    }
}
Run Code Online (Sandbox Code Playgroud)

数据项:

public class DataItem
{
    public bool IsChecked { get; set; }

    public string Text { get; set; }

    public string Color { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明