如何在分隔符后将ComboBox项目对齐到右侧

时间:2012-08-01 02:13:35

标签: wpf combobox styles controltemplate datatrigger

最初,ComboBox项目左对齐。我向ComboBox添加了一个分隔符,只要显示数据绑定的空值,分隔符就会可见。我想将分隔符后面的ComboBox项对齐。这有可能成功吗?

<cc:TogglingComboBox  FontSize="11" Grid.Column="1" InitialDisplayItem="10"  HorizontalAlignment="Stretch" SelectedValue="{Binding Dye}"  ItemsSource="{Binding Dyes}" Margin="5,3,5,0">
<ComboBox.ItemContainerStyle>                                 
<Style TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
    <Style.Triggers>

        <DataTrigger Binding="{Binding}" Value="{x:Null}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>

1 个答案:

答案 0 :(得分:2)

对于DataContext

ComboBoxItem是某些集合的对象。如我所见,每当集合的源项为空时,将应用Separator模板。如果您知道集合中有固定数量的空值项(例如1),则可以编写Converter

这是一个例子:

MainWindow.xaml

<Window x:Class="ComboBoxWithSeparator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ComboBoxWithSeparator"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <local:MyConverter x:Key="MyConverter" />
            <Style TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
                <Setter Property="HorizontalAlignment">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource MyConverter}">
                            <Binding />
                            <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ComboBox}" Path="ItemsSource" />
                        </MultiBinding>
                    </Setter.Value>
                </Setter> 
                <Style.Triggers>
                    <DataTrigger Binding="{Binding}" Value="{x:Null}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                    <Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="HorizontalAlignment" Value="Stretch" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <ComboBox ItemsSource="{Binding}"
                  Width="250"
                  Height="25"/>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<string> a = new List<string>()
        {
            "one",
            "two",
            "three",
            "four",
            "five",
            null,
            "seven",
            "eight",
            "nine",
            "ten"
        };
        DataContext = a;
    }
}
public class MyConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        string s = (string)values[0];
        IEnumerable<string> array = (IEnumerable<string>)values[1];

        if (s == null)
            return HorizontalAlignment.Stretch;

        foreach (string item in array)
        {
            if (s == item)
                return HorizontalAlignment.Left;
            if (item == null)
                return HorizontalAlignment.Right;
        }

        return HorizontalAlignment.Left;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

结果:

ComboBox items are aligned to the right after the separator