代码之家  ›  专栏  ›  技术社区  ›  Sonny Boy

WPF-复选框组合框-是否选择绑定?

  •  1
  • Sonny Boy  · 技术社区  · 14 年前

    我已经实现了一个复选框组合框,在GUI中看起来很不错,但在功能性方式中使用它时遇到了问题。

    我遇到的主要问题是弄清楚哪些箱子是真的被检查过的。在运行时,ComboBox.SelectedItem工作正常,但是如果我遍历所有项,它们总是返回IsSelected==false。

    有什么想法吗?

    这是我的xaml:

    <ComboBox Name="cboParam3" Grid.Row="0" Grid.Column="5" SelectedValuePath="Key" KeyDown="headerBar_KeyDown">
       <ComboBox.ItemTemplate>
          <DataTemplate>
             <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Path=IsSelected}" VerticalAlignment="Center" Margin="0,0,4,0" />
                <TextBlock Text="{Binding Value}" VerticalAlignment="Center"/>
             </StackPanel>
          </DataTemplate>
       </ComboBox.ItemTemplate>
    </ComboBox>
    

    下面是我最初填充组合框的代码:

    Dictionary<int, string> codes = CodeCache.CodeLookup<DACaseCategoryCode>();
    List<MultipleComboItem> items = new List<MultipleComboItem>();
    
    codes.ToList().ForEach(t =>
    {
       MultipleComboItem item = new MultipleComboItem();
    
       item.Key = t.Key;
       item.Value = t.Value;
       item.IsSelected = false;
    
       items.Add(item);
    });
    
    this.lblParam3.Content = "Case Category:";
    this.cboParam3.ItemsSource = items;
    

    我还需要加入其他的东西来让它发挥作用吗?

    谢谢,
    桑尼

    附言 MultipleComboItem 只是一个有三个性质的简单结构。没什么奇怪的事发生。

    4 回复  |  直到 14 年前
        1
  •  0
  •   Scott    14 年前

    编辑:

    我编写了一个快速测试应用程序,绑定似乎可以使用以下方法正常工作(但是,选中复选框不会设置组合框的SelectedItem属性):

    <Window x:Class="TestApp11.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:l="clr-namespace:TestApp11"
      Title="Window1" >
        <Window.Resources>
        </Window.Resources>
        <Grid Background="Black">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <ComboBox Name="cboParam3" Grid.Row="0" Grid.Column="0">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding Path=Select}" VerticalAlignment="Center" Margin="0,0,4,0" />
                            <TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
                </ComboBox>
            <Button Grid.Row="1" Content="Click Me For Break Point" Click="Button_Click"></Button>
        </Grid>
    </Window>
    
    
    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace TestApp11
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<MCI> MCIList { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            this.MCIList = new ObservableCollection<MCI>();
            this.cboParam3.ItemsSource = this.MCIList;
    
            this.MCIList.Add(new MCI());
            this.MCIList.Add(new MCI());
            this.MCIList.Add(new MCI());
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
    
        }
    
    }
    
    public class MCI
    {
        public bool Select { get; set; }
        public string Name { get; set; }
        public MCI()
        {
            this.Name = "Bob";
            this.Select = false;
        }
    }
    }
    

    你做的和我上面说的有什么不同吗?

        2
  •  1
  •   DaveShaw Thishin    13 年前
    <ComboBox Name="identifiercombo" Text="{Binding SelectedIdentifier, UpdateSourceTrigger=PropertyChanged}"  ItemsSource="{Binding IdentifierCollection}"  SelectedIndex="0" IsEditable="True" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Stretch" Margin="10,5">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox Click="CheckBox_Click" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center"/>                            
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    
        3
  •  0
  •   Vladimir Dorokhov    14 年前

    ComboBox.SelectedItem工作正常

    好的,你可以选择多个目标。

    如果我遍历所有项,它们总是返回IsSelected==false。

    这是因为您已经绑定了IsChecked=“{Binding Path=IsSelected}”,如果选中打开的组合框列表中的复选框并再次遍历这些项,则可以找到IsSelected==true的项。

    所以,如果不选中复选框,就找不到IsSelected==true。

    你一定感觉不一样 挑选出来的 组合框项和 选中的 显示项目的复选框。

        4
  •  0
  •   Rashed Bhatti    11 年前

    我需要创建一个由复选框组成的自定义组合框,以显示供用户选择的日历月列表。此应用程序允许用户选择月份,无论何时选择或取消选择月份,都会更新列表框。我遍历组合框的itemsource observate集合,以确定选中的月份。我可能可以使用LINQ来查询集合,而不是手动遍历可观察集合,但那是另一天的事了。如果您想测试这个,只需创建一个名为CustomComboBox的新wpf应用程序(在C中),然后将xaml和C复制并粘贴到您的应用程序中:

    <Window x:Class="CustomComboBox.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
    
        </Window.Resources>
        <Grid>      
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30" />
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />            
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0" Grid.Row="0" Content="Select the months:" />
                <Label Grid.Column="1" Grid.Row="1" Content="Months selected:" />
                <ComboBox x:Name="ComboBoxMonths" Grid.Column="0" Grid.Row="1">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox IsChecked="{Binding Path=monthSelected}" VerticalAlignment="Center" Margin="0,0,4,0" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
                                <TextBlock Text="{Binding monthName}" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
                <ListBox x:Name="ListBoxMonthsChecked" Grid.Column="1" Grid.Row="2">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding monthName}" VerticalAlignment="Center"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>       
        </Grid>
    
    </Window>
    

    c代码:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace CustomComboBox
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public static ObservableCollection<Month> monthList = new ObservableCollection<Month>();
            public static ObservableCollection<Month> monthsChecked = new ObservableCollection<Month>();
    
            public MainWindow()
            {
                InitializeComponent();
    
                this.ComboBoxMonths.ItemsSource = monthList;
                this.ListBoxMonthsChecked.ItemsSource = monthsChecked;
    
                //add Months to the ComboBoxMonths
                monthList.Add(new Month() { monthSelected = false, monthName = "January", monthNumber = 01 });
                monthList.Add(new Month() { monthSelected = false, monthName = "February", monthNumber = 02 });
                monthList.Add(new Month() { monthSelected = false, monthName = "March", monthNumber = 03 });            
                monthList.Add(new Month() { monthSelected = false, monthName = "April", monthNumber = 04 });
                monthList.Add(new Month() { monthSelected = false, monthName = "May", monthNumber = 05 });
                monthList.Add(new Month() { monthSelected = false, monthName = "June", monthNumber = 06 });
                monthList.Add(new Month() { monthSelected = false, monthName = "July", monthNumber = 07 });
                monthList.Add(new Month() { monthSelected = false, monthName = "August", monthNumber = 08 });
                monthList.Add(new Month() { monthSelected = false, monthName = "September", monthNumber = 09 });
                monthList.Add(new Month() { monthSelected = false, monthName = "October", monthNumber = 10 });
                monthList.Add(new Month() { monthSelected = false, monthName = "November", monthNumber = 11 });
                monthList.Add(new Month() { monthSelected = false, monthName = "December", monthNumber = 12 });
            }
    
            public class Month
            {
                public string monthName { get; set; } 
                public int monthNumber { get; set; } 
    
                private bool _monthSelected;
                public bool monthSelected //the checkbox is bound to this
                {
                    get
                    {
                        return _monthSelected;
                    }
                    set
                    {
                        if (value != this._monthSelected)
                        {
                            _monthSelected = value;                        
                        }
                    }
                }
    
            }
    
            private void CheckBox_Checked(object sender, RoutedEventArgs e)
            {
                monthsChecked.Clear();
                foreach (Month m in monthList)
                {
                    if (m.monthSelected == true)
                    {                    
                        monthsChecked.Add(m);
                    }
                }            
            }
    
            private void CheckBox_UnChecked(object sender, RoutedEventArgs e)
            {
                monthsChecked.Clear();
                foreach (Month m in monthList)
                {
                    if (m.monthSelected == true)
                    {
                        monthsChecked.Add(m);
                    }
                }
            }
    
        }
    }