簡體   English   中英

如何為下拉列表創建不同的ComboBox ItemTemplate

[英]How to create different ComboBox ItemTemplate for dropdown

如何在我的UWP應用中為ComboBox定義自定義模板,對於下拉菜單,我需要帶有標簽的復選框,但是當用戶選擇任何選項時,我只需要在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>

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

轉換器類

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

    }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM