2012-08-01 67 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

相關問題