Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 3.33 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to hide combobox toggle button if there is only one item?
  2. <ComboBox x:Name="CList" ItemsSource="{Binding Path=C}"  >                    
  3.     <Style TargetType="{x:Type ToggleButton}" >
  4.         <Style.Triggers>
  5.             <DataTrigger Binding="{Binding Path=Items.Count, ElementName=CList}" Value="1">
  6.                 <Setter Property="Visibility" Value="Hidden" />
  7.             </DataTrigger>
  8.         </Style.Triggers>
  9.     </Style>
  10. </ComboBox>
  11.        
  12. using System;
  13. using System.Windows;
  14. using System.Windows.Data;
  15. using System.Globalization;
  16.  
  17. namespace WPFSandbox
  18. {
  19.     public class ComboBoxItemCountToEnabledConverter : IValueConverter
  20.     {
  21.         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  22.         {
  23.             if (value != null && value.GetType() == typeof(Int32))
  24.             {
  25.                 if ((int)value > 1)
  26.                     return true;
  27.             }
  28.  
  29.             return false;
  30.         }
  31.  
  32.         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  33.         {
  34.             throw new NotImplementedException();
  35.         }
  36.     }
  37.  
  38.     public class ComboBoxItemCountToVisibilityConverter : IValueConverter
  39.     {
  40.         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  41.         {
  42.             if (value != null && value.GetType() == typeof(Int32))
  43.             {
  44.                 if ((int)value > 1)
  45.                     return Visibility.Visible;
  46.             }
  47.  
  48.             return Visibility.Collapsed;
  49.         }
  50.  
  51.         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  52.         {
  53.             throw new NotImplementedException();
  54.         }
  55.     }
  56. }
  57.        
  58. <Window
  59. ...
  60. ...
  61. xmlns:converters="clr-namespace:WPFSandbox">
  62.  
  63. <Window.Resources>
  64.     <converters:ComboBoxItemCountToVisibilityConverter x:Key="ComboBoxItemCountToVisibilityConverter"/>
  65.     <converters:ComboBoxItemCountToEnabledConverter x:Key="ComboBoxItemCountToEnabledConverter"/>
  66. </Window.Resources>
  67.  
  68. <StackPanel>
  69.    <ComboBox ItemsSource="{Binding C}" IsEnabled="{Binding Path=C.Count, Converter={StaticResource ComboBoxItemCountToEnabledConverter}}"/>
  70.    <ToggleButton Visibility="{Binding Path=C.Count, Converter={StaticResource ComboBoxItemCountToVisibilityConverter}}"/>
  71. </StackPanel>
  72.        
  73. <ComboBox Name="CList" ItemsSource="{Binding Path=C}"
  74.                      SelectedItem="{Binding Path=CC}" VerticalAlignment="Center" Margin="0,0,10,0" >
  75.                     <ComboBox.Style>
  76.                         <Style TargetType="{x:Type ComboBox}" >
  77.                             <Style.Triggers>
  78.                                 <DataTrigger Binding="{Binding Path=Items.Count, ElementName=CList}" Value="1">
  79.                                     <Setter Property="Template">
  80.                                         <Setter.Value>
  81.                                             <ControlTemplate>
  82.                                                 <TextBlock Text="{Binding Items[0], ElementName=CList}" />
  83.                                             </ControlTemplate>
  84.                                         </Setter.Value>
  85.                                     </Setter>
  86.                                 </DataTrigger>
  87.                             </Style.Triggers>
  88.                         </Style>
  89.                     </ComboBox.Style>
  90.                 </ComboBox>