2016-04-30 31 views
0

如何定義自定義模板,在我UWP應用組合框,爲下拉我需要標籤複選框,但創建不同的組合框的ItemTemplate當用戶選擇任何選項,我只需要顯示標籤在comobox如何爲下拉

我試過,但我沒有工作:

<ComboBox x:Name="cbCountry" 
         Header="Country" 
         Margin="10,5" 
         HorizontalAlignment="Stretch" 
         Style="{StaticResource ComboBoxStyle}"> 
       <ComboBox.ItemTemplate> 
        <DataTemplate> 
         <CheckBox Content="{Binding}"></CheckBox> 
        </DataTemplate> 
       </ComboBox.ItemTemplate> 
       <ComboBox.ItemsPanel> 
        <ItemsPanelTemplate> 
         <TextBlock Text="{Binding}"/> 
        </ItemsPanelTemplate> 
       </ComboBox.ItemsPanel> 

+0

僅使用一個模板,複選框可見性取決於它是否將selectedItem或不被處理。你可以使用轉換器的那個 – Archana

+0

我是UWP的新手,一些代碼演示會有幫助。 –

+0

好的。我會在稍後添加樣本。 – Archana

回答

1
<Page.Resources> 
     <local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> 
</Page.Resources> 
<ComboBox x:Name="cbState" DropDownClosed="cbState_DropDownClosed" DropDownOpened="cbState_DropDownOpened" Margin="75,287,0,0" Width="169" ItemContainerStyle="{StaticResource ComboBoxItemStyle1}" Style="{StaticResource ComboBoxStyle1}" > 

       <ComboBox.ItemTemplate> 
        <DataTemplate> 
        <StackPanel> 
         <CheckBox Content="{Binding}" 
            Visibility="{Binding IsCheckBoxVisible, Mode=TwoWay, Converter={StaticResource BooleanToVisibilityConverter}, UpdateSourceTrigger=PropertyChanged}" > 
          </CheckBox> 
         <TextBlock Text="{Binding State_Name}"/> 
        </StackPanel> 
       </DataTemplate> 
       </ComboBox.ItemTemplate> 

      </ComboBox> 

    private void cbState_DropDownClosed(object sender, object e) 
     { 
      foreach (var item in (sender as ComboBox).Items) 
      { 
       (item as State).IsCheckBoxVisible = false; 
      } 
     } 

     private void cbState_DropDownOpened(object sender, object e) 
     { 
      foreach(var item in (sender as ComboBox).Items) 
      { 
       (item as State).IsCheckBoxVisible = true; 
      } 
     } 

Converter類

public class BooleanToVisibilityConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, string language) 
     { 
      var boolValue = System.Convert.ToBoolean(value); 

      return boolValue ? Visibility.Visible : Visibility.Collapsed; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, string language) 
     { 
      return ((Visibility)value == Visibility.Visible) ? true : false; 
      ; 
     } 
    } 

模型類。執行INotifyPropertyChanged反映在UI(複選框可見性的更改)

public class State:INotifyPropertyChanged 
    { 
     public string State_Name { get; set; } 
     public object State_Id { get; set; } 
     bool isCheckBoxVisible; 
     public bool IsCheckBoxVisible 
     { 
      get { return isCheckBoxVisible; } 
      set 
      { 
       if (value != isCheckBoxVisible) 
       { 
        isCheckBoxVisible = value; 
        OnPropertyChanged("IsCheckBoxVisible"); 
       } 
      } 
     } 
     public State(string name,object id,bool visibility=false) 
     { 
      State_Name = name; 
      State_Id = id; 
      IsCheckBoxVisible = false; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public override string ToString() 
     { 
      return State_Name; 
     } 
     void OnPropertyChanged(string propertyName) 
     { 
      // the new Null-conditional Operators are thread-safe: 
      this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 

    }