WPF ComboBox自定义显示所选项目

时间:2015-12-07 18:58:32

标签: c# wpf xaml combobox multi-select

我按照在线教程为wpf项目提供多选 ListBox以包含复选框。我可以在后端相应地操作选定的值,但是当折叠到我想要的任何文本时,我无法找到解决方案来设置控件上的显示值。

XAML

<ComboBox x:Name="chSel_0">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <CheckBox x:Name="key_0" IsChecked="{Binding IsIncluded}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
        <TextBlock Text="{Binding Channel}"/>
      </StackPanel>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

例如,如果我从可用项目中选择复选框1,4,6,我希望ComboBox在折叠时显示1,4,6。现在虽然我满足于能够在没有'IsEditable = true&#39;的情况下在折叠的控制器上放置任何文本。在ComboBox标签上。

更新

我一直在编辑模板,并且几乎已经完成了工作&#34;

XAML

<ContentPresenter DataContext="Binding" Content="{Binding KeysCfgChannels[0]}"/>

C#

public MainWindow()
{
    DataContext = this;
    InitializeComponent();
    CfgChannels();
}

public ObservableCollection<string> KeysCfgChannels { get; private set; }

public void CfgChannels()
{

    string val = "";
    this.KeysCfgChannels = new ObservableCollection<string> { };

    for (int i = 0; i < 16; i++)
      {
        foreach (ChSelVal ch in chCfgs[i])
        {
          if (ch.IsIncluded)
          {
            val += ch.Channel;
          }
        }
        KeysCfgChannels.Add(val);
        //val = "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16";               
    }
}

问题是它只显示新字符串如果它隐式声明,如果我KeysCfgChannels.Add("0 1 2 3")将会显示但是它不会起作用我有如图所示。

1 个答案:

答案 0 :(得分:0)

查看下一个链接here(#1,012 - 使用面向ComboBox的不同数据模板),它应该对您有所帮助。

<强>更新 看这里的组合多选: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

如果您遇到代码问题,我仍然很乐意提供帮助。 问候。

相关问题