2017-11-06 66 views
-2

내 XAML 코드wpf에서 다중 선택 콤보 박스에 여러 개의 체크 된 항목을 표시하는 방법은 무엇입니까?

<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 way 또는 xaml.cs 방법에 대한 도움이 있으면 대단히 감사하겠습니다.

답변

0
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을하고있는 코드를 직접 추가 할 수도 있고, 당신은 그것을

이라고 부를 수도 있습니다.