2013-06-27 27 views
0

我所做的是左圖顯示的效果,我有一個包含兩組數據的組合框,我想要的是當我選擇組合框中的項目時,顯示的文字將看起來像所顯示的右側圖像(在選定的項目之前添加一些前綴),例如我選擇項目''in Group A然後顯示的文字將是'A_00',如何在我的代碼基礎上實現這個效果? enter image description here如何在自定義樣式的組合框中顯示替代文本

在GroupComboBoxResDic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:helper="clr-namespace:WpfApplication3"> 

    <!-- Main style for ComboBox --> 
    <Style x:Key="MyComboBox" TargetType="{x:Type ComboBox}">   

     <Setter Property="ItemsPanel"> 
      <Setter.Value> 
       <ItemsPanelTemplate> 
        <WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="150" Height="Auto" /> 
       </ItemsPanelTemplate> 
      </Setter.Value> 
     </Setter> 

     <Setter Property="ItemTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <TextBlock Text="{Binding Item, Converter={StaticResource terminalToDisplayNameConverter}, Mode=OneWay}" Width="40"/> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <!-- Style for ComboBoxItem --> 
    <Style TargetType="{x:Type ComboBoxItem}"> 
     <Setter Property="Width" Value="50" /> 
    </Style> 

    <!-- Style for ItemContainerStyle --> 
    <Style x:Key="ComboBoxItemContainerStyle" TargetType="{x:Type ComboBoxItem}"> 
     <Setter Property="IsEnabled" Value="{Binding Available}" /> 
    </Style> 

    <!-- DataTemplate for HeaderTemplate --> 
    <DataTemplate x:Key="MyHeaderTemplate"> 
     <Border BorderBrush="Black" BorderThickness="2"> 
      <TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" Background="YellowGreen" /> 
     </Border> 
    </DataTemplate> 

</ResourceDictionary> 

XAML文件

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication3"> 

    <Window.Resources> 
     <ResourceDictionary Source="GroupComboBoxResDic.xaml" /> 
    </Window.Resources> 

    <Grid> 
     <ComboBox x:Name="MyComboBox1" 
        Style="{StaticResource MyComboBox}" 
        IsSynchronizedWithCurrentItem="False" 
        ItemContainerStyle="{StaticResource ComboBoxItemContainerStyle}"     
        ItemsSource="{Binding InputsList}" Margin="293,50,104,234"> 
      <ComboBox.GroupStyle> 
       <GroupStyle HeaderTemplate="{StaticResource MyHeaderTemplate}" /> 
      </ComboBox.GroupStyle> 
     </ComboBox> 
    </Grid> 
</Window> 

代碼隱藏文件

public enum CategoryGroup 
    { 
     GroupA = 0, 

     GroupB, 
    } 


    public class CategoryItemString 
    { 
     public string Item { get; set; } 
     public bool Available { get; set; } 

     public CategoryGroup Group { get; set; } 

     public string Category 
     { 
      get 
      { 
       switch (Group) 
       { 
        case CategoryGroup.GroupA: 
         return "Group A"; 
        case CategoryGroup.GroupB: 
         return "Group B"; 
       } 

       return string.Empty; 
      } 
     } 

     public override bool Equals(object obj) 
     { 
      if (obj == null) return false; 
      CategoryItemString rht = obj as CategoryItemString; 
      if (rht == null) return false; 

      return rht.Item.Equals(this.Item) && 
       rht.Group == this.Group; 
     } 
    } 


    public class VM : INotifyPropertyChanged 
    { 
     private ListCollectionView lcv; 

     public VM() 
     { 

      List<CategoryItemString> items = new List<CategoryItemString>(); 

      for (int i = 0; i < 18; ++i) 
      { 
       items.Add(new CategoryItemString { Item = string.Format("{0:D2}", i), Available = (true), Group = CategoryGroup.GroupA }); 
      } 

      for (int i = 0; i < 4; ++i) 
      { 
       items.Add(new CategoryItemString { Item = string.Format("{0:D2}", i), Available = (true), Group = CategoryGroup.GroupB }); 
      } 

      //Need the list to be ordered by the category or you might get repeating categories 
      lcv = new ListCollectionView(items.OrderBy(w => w.Category).ToList()); 

      //Create a group description 
      lcv.GroupDescriptions.Add(new PropertyGroupDescription("Category")); 
     } 

     public ListCollectionView InputsList 
     { 
      get { return lcv; } 
      set 
      { 
       if (lcv != value) 
       { 
        lcv = value; 
        OnPropertyChanged("InputsList"); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string prop) 
     { 
      if (this.PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
     } 
    } 

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

      VM vm = new VM(); 

      this.MyComboBox1.DataContext = vm; 
     } 
    } 

回答

0

你需要做的是修改的模板ComboBox

首先創建用於SelectionBox模板(一個ContentControlComboBox的模板顯示當前選擇的項目):

<DataTemplate x:Key="mySelectionBoxItemTemplate"> 
    <TextBlock Text="{Binding Path=DisplayValue}" /> 
</DataTemplate> 

然後在CategoryItemString創建DisplayValue屬性:

public string DisplayValue 
{ 
    get { return String.Format("{0}_{1}", Group, Item); } 
} 

編輯ComboBox的默認模板並修改SelectionBox的ControlTemaplate,請在下面的代碼中查找註釋以查找所做的更改:

<ControlTemplate x:Key="ComboBoxControlTemplate1" TargetType="{x:Type ComboBox}"> 
    <Grid x:Name="MainGrid" SnapsToDevicePixels="True"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/> 
     </Grid.ColumnDefinitions> 
     <Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
      <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}"> 
       <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"> 
        <ScrollViewer x:Name="DropDownScrollViewer"> 
         <Grid RenderOptions.ClearTypeHint="Enabled"> 
          <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> 
           <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/> 
          </Canvas> 
          <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </Grid> 
        </ScrollViewer> 
       </Border> 
      </Themes:SystemDropShadowChrome> 
     </Popup> 
     <ToggleButton BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> 
      <ToggleButton.Style> 
       <Style TargetType="{x:Type ToggleButton}"> 
        <Setter Property="OverridesDefaultStyle" Value="True"/> 
        <Setter Property="IsTabStop" Value="False"/> 
        <Setter Property="Focusable" Value="False"/> 
        <Setter Property="ClickMode" Value="Press"/> 
        <Setter Property="Background" Value="Transparent"/> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type ToggleButton}"> 
           <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="True"> 
            <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"> 
             <Path x:Name="Arrow" Data="M0,0L3.5,4 7,0z" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/> 
            </Grid> 
           </Themes:ButtonChrome> 
           <ControlTemplate.Triggers> 
            <Trigger Property="IsChecked" Value="True"> 
             <Setter Property="RenderPressed" TargetName="Chrome" Value="True"/> 
            </Trigger> 
            <Trigger Property="IsEnabled" Value="False"> 
             <Setter Property="Fill" TargetName="Arrow" Value="#FFAFAFAF"/> 
            </Trigger> 
           </ControlTemplate.Triggers> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </ToggleButton.Style> 
     </ToggleButton> 
     <!-- Only the ContentTemplate below was changed to a static resurce--> 
     <ContentPresenter ContentTemplate="{StaticResource mySelectionBoxItemTemplate}" 
          Content="{TemplateBinding SelectionBoxItem}" 
          ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          IsHitTestVisible="False" 
          Margin="{TemplateBinding Padding}" 
          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True"> 
      <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/> 
      <Setter Property="Color" TargetName="Shdw" Value="#71000000"/> 
     </Trigger> 
     <Trigger Property="HasItems" Value="False"> 
      <Setter Property="Height" TargetName="DropDownBorder" Value="95"/> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="False"> 
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
      <Setter Property="Background" Value="#FFF4F4F4"/> 
     </Trigger> 
     <MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="IsGrouping" Value="True"/> 
       <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="False"/> 
      </MultiTrigger.Conditions> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
     </MultiTrigger> 
     <Trigger Property="CanContentScroll" SourceName="DropDownScrollViewer" Value="False"> 
      <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/> 
      <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

最後一件事 - 上面添加模板在MyComboBoxStyle

 <Setter Property="Template" Value="{StaticResource ComboBoxControlTemplate1}" /> 
+0

如何改變我現有的代碼庫? –

相關問題