2014-10-30 139 views
4

我正在使用Xceed checkable combobox。現在我想顯示一個默認文本,取決於組合框中選中的複選框,但我不知道該怎麼做。可選組合框的默認文本

例如:

enter image description here

文本框的內容(紅色箭頭)應該是:

  • 如果沒有選擇: 「請選擇」
  • 如果一切都被選中:「所有人」
  • 如果一個或多個被選擇: 「特定選擇」

喜歡:

enter image description here


例-代碼:

XAML:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <xctk:CheckComboBox x:Name="_checkComboBox" 
          Height="22" 
          VerticalAlignment="Stretch" 
          ItemsSource="{Binding Names}" 
          SelectedItemsOverride="{Binding SelectedNames}" 
          DisplayMemberPath="Title" 
          Delimiter=", " 
          Width="100"/> 
    </Grid> 
</Window> 

CS:

using System.Windows; 

namespace WpfApplication1 
{ 
    using System.Collections.ObjectModel; 

    public partial class MainWindow 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      _checkComboBox.DataContext = this; 

      Names = new ObservableCollection<People>() 
       { 
       new People() { Title = "Mikel" }, 
       new People() { Title = "Tom" }, 
       new People() { Title = "Jennifer" }, 
       new People() { Title = "Megan" }, 
       }; 

      SelectedNames = new ObservableCollection<People>(); 
     } 

     public ObservableCollection<People> Names 
     { 
      get; 
      set; 
     } 

     public ObservableCollection<People> SelectedNames 
     { 
      get; 
      set; 
     } 
    } 

    public class People 
    { 
     public string Title 
     { 
      get; 
      set; 
     } 
    } 
} 
+1

點我們默認的樣式模板或右鍵單擊 - 拉出>編輯模板,組合框的通常只是一個切換按鈕和排序的一個ItemsControl,可以在切換按鈕的內容部分拋出一些觸發器並獲得您的解決方案。 – 2014-10-30 17:57:11

回答

5

使用值轉換器。

像這樣的東西應該工作:

XAML:

SelectedItemsOverride="{Binding SelectedNames, 
         Converter={StaticResource SelectedNamesConverter}}, 
         ConverterParameter={Binding Names}}" 

C#:

public class SelectedNamesConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if ((List<string>)value.length() == 0): 
      return "Please select"; 

     if (((List<string>)value).length() == ((List<string>)parameter).length()) 
      return "All people"; 

     return "Specific selection"; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     // Log this as a non-fatal error. You shouldn't be here! 
     return DependencyProperty.UnsetValue; 

     // Alternatively: 
     // throw new NotImplementedException(); 
    } 
} 

而且使名稱的依賴項屬性:

public static DependencyProperty NamesProperty = DependencyProperty.Register(
     "Names", 
     typeof(ObservableCollection<People>), 
     typeof(MainWindow)); 

    public ObservableCollection<People> Names 
    { 
     get { return (ObservableCollection)GetValue(NamesProperty); } 
     private set { SetValue(NamesProperty, value); } 
    } 

製作主窗口繼承德普endencyObject:

public class MainWindow : DependencyObject 
+0

我收到此錯誤:無法在'Binding'類型的'ConverterParameter'屬性上設置'綁定'。 '綁定'只能在DependencyObject的DependencyProperty上設置。 – Stampy 2014-11-04 09:05:42

+0

@Stampy我做了一些編輯。試試嗎? – Zaaier 2014-11-04 09:13:37

+0

我試過了,但是:名稱「SetValue」在currect上下文中不存在(與GetValue相同) – Stampy 2014-11-04 09:20:25