2012-08-01 87 views
0

最初,ComboBox项目对齐到左侧。我向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> 

+0

我想你Ç找不到具体的元素,所以我建议你使用StyleSelector并且自己检查元素在代码中的位置(在null之前或之后) – 2012-08-01 03:34:44

+0

我不太确定,因为我对wpf很陌生。怎么做? – DEN 2012-08-01 04:47:41

回答

2

DataContextComboBoxItem是一些收集的对象。正如我所看到的,您的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

+0

无法投射类型为'System.Collections.Generic.List'1 [System.Object]'的对象以键入'System.Collections.Generic.IEnumerable'1 [System.String]'。 在这行代码“IEnumerable array =(IEnumerable )values [1];” – DEN 2012-08-01 05:39:19

+0

我写了一个例子,并且我发送了一个DataContext列表。在你的情况下,你可以将这些IEnumerable更改为你的清单,并按照你的需要进行投射。 – stukselbax 2012-08-01 05:44:31

+0

请看这个例子:http://stackoverflow.com/questions/11680660/combobox-show-and-hide-combobox-items-in-wpf。该示例是可以显示和隐藏ComboBox项目的自定义组合框。所以无论何时点击More按钮,剩余的ComboBox项目都会显示出来。但我想在其余项目之间和初始显示项目之后添加分隔符。那些剩余的项目将对齐到右侧,而初始显示项目仍然保持向左对齐。该示例是从ComboBox继承的自定义ComboBox。这有可能实现吗? – DEN 2012-08-01 06:03:01