【问题标题】:How to display multiple checked items in multiselect combobox in wpf?如何在wpf的多选组合框中显示多个选中的项目?
【发布时间】:2018-04-18 07:29:12
【问题描述】:

我的 Xaml 代码

<ComboBox HorizontalContentAlignment="Left"
                              x:Name="Stat"
                              IsEditable="True"
                              Width="247"
                              HorizontalAlignment="Left"
                              IsReadOnly="True"
                              ItemsSource="{Binding OrderStatus,Mode=TwoWay}"                                                      
                         Text="{Binding StatusSelectedValue}"  

                          Height="26">

                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox  Content="{Binding Description}"
                                       IsChecked="{Binding IsChecked}"
                                       Checked="CheckBox_Checked"
                                       Unchecked="CheckBox_Unchecked"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

xaml.cs

//当项目被选中或取消选中时,我想更新我的多选组合框上显示的文本

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        var selectedItem = sender as CheckBox ;
        var r= selectedNode.IsChecked.Value;

//尝试获取所有选中项的值并将其连接并在组合框中显示类似于下面的内容

            Stat.Text = String.Join(",",selectedItemValues.toArray());//this line of code is for example only for what i want to do
    }

非常感谢任何 mvvm 方式或 xaml.cs 方式的帮助。

【问题讨论】:

    标签: c# wpf xaml mvvm combobox


    【解决方案1】:
     public void SetText()
        {
    
            if (this.SelectedItems != null)
            {
                StringBuilder displayText = new StringBuilder();
                foreach (ComboItem s in comboItemsCollection)
                {
                    if (s.IsSelected == true && s.Title == Properties.Resources.CHECKALL)
                    {
                        displayText = new StringBuilder();
                        displayText.Append("All");
                        break;
                    }
                    else if (s.IsSelected == true && s.Title != Properties.Resources.CHECKALL)
                    {
                        displayText.Append(s.Title);
                        displayText.Append(',');
                    }
                }
                this.Text = displayText.ToString().TrimEnd(new char[] { ',' });
            }
            // set DefaultText if nothing else selected
            if (string.IsNullOrEmpty(this.Text))
            {
                this.Text = this.DefaultText;  
            }
        }
    

    这就是我所做的。它用“;”显示所有选定的项目作为分隔符。如果您有一个“全部”复选框或选择全部,它只会显示全部。当然我不知道您使用的是什么对象,所以我保留了我的代码(您需要更改循环部分,特别是 ComboItem 部分)

    请注意,这是一个函数(当然),因此您可以在执行 String.Join 的位置直接添加代码,也可以调用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多