简体   繁体   English

WPF ComboBox自定义显示所选项目

[英]WPF ComboBox Custom Display Selected Items

I followed an online tutorial to have a multi-select ListBox for a wpf project to include check boxes. 我遵循了在线教程,为wpf项目提供了一个多选 ListBox ,以包括复选框。 I am able to manipulate the selected values accordingly on the back-end but I can't find a solution to set the displayed value on the control when collapsed to whatever text I want. 我可以在后端相应地操作选定的值,但是找不到折叠到所需文本时在控件上设置显示值的解决方案。

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>

For example if I select check box 1, 4, 6 from the available items I would like for the ComboBox to display something like 1,4,6 when collapsed . 例如,如果我从可用项目中选择复选框1、4、6,我希望ComboBox 折叠时显示1,4,6之类的内容。 For now though I would be content to be able to put any text on the collapsed controller without having `IsEditable=true' on the ComboBox tag. 就目前而言,尽管我很满足于能够在折叠的控制器上放置任何文本而在ComboBox标记上不具有“ IsEditable = true”的条件。

UPDATE UPDATE

I've been editing the template and got this to almost "work" 我一直在编辑模板,并且几乎可以“正常工作”

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";               
    }
}

The issue is it will only display the new string if it's implicitly declared as in it will display if I do KeysCfgChannels.Add("0 1 2 3") but it won't work if I have it as shown. 问题是, 如果隐式声明,它将仅显示新字符串,因为如果我执行KeysCfgChannels.Add("0 1 2 3")它将显示新字符串,但如果显示如图所示,它将无法正常工作。

Look at the next link here (#1,012 – Using a Different Data Template for the Face of a ComboBox), it should help you. 请看这里的下一个链接(#1,012 –对组合框的外观使用其他数据模板),它应该会对您有所帮助。

Update look here for combo multiselection: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!) . 更新此处以查看组合的多重选择: WPF:带有CheckBoxes作为项目的ComboBox(甚至可以即时更新!)

I'll still be glad to help if you will have the problem with the code. 如果您的代码有问题,我仍然很乐意为您提供帮助。 Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM