【问题标题】:how to get the value of checkbox which is inside the combo box WPF?如何获取组合框WPF内的复选框的值?
【发布时间】:2018-02-05 08:49:40
【问题描述】:

我有一个组合框,复选框位于组合框内。我想要多选复选框的值。我的代码:

<ComboBox Name="LocationFilterComboBox" Width="100" SelectedItem="{Binding LocationValue}">
   <ComboBox.ItemTemplate >
      <DataTemplate>
         <StackPanel Orientation="Horizontal" >
            <CheckBox Content="{Binding LocationValue}"  IsChecked="{Binding ElementName=all, Path=IsChecked, Mode=TwoWay}" Width="120" />
         </StackPanel>
      </DataTemplate>
   </ComboBox.ItemTemplate>
</ComboBox>

后端代码:

//code to get the value
public partial class Location
{
    #region Property
    private string LOCAID;
    public string LocaId
    {
        get
        {
            return LOCAID;
        }
        set
        {
            value = LOCAID;
        }
    }

    private string LOCADESC;
    public string LocationValue
    {
        get
        {
            return LOCADESC;
        }
        set
        {
            value = LOCADESC;
        }
    }
    #endregion
}
}

//code for binding the location
public IList<Location> BindAllLocation()
{
    if (Repository != null) Repository.Dispose();
    Repository = GetInvoiceRepository();
    IList<Location> locationList = Repository.GetLocations(((App)Application.Current).DataContextFactory);

    return locationList;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    LocationFilterComboBox.ItemsSource = BindAllLocation();
}

【问题讨论】:

  • 我不明白你的问题。您已经在使用绑定。假设您的绑定,您的值应该在属性IsChecked 中;)
  • 你分享代码示例我如何检索检查项目@MightyBadaboom
  • @AkhilJain 请发布我们可以用作示例的代码。
  • @lightlike 代码已更新
  • @AkhilJain 对不起。我的意思是你绑定到的结构(属性,...)。

标签: wpf checkbox combobox


【解决方案1】:

我不会直接设置 ItemsSource。尝试绑定它:

private ObservableCollection<Location> _locationList = new ObservableCollection<Location>();
public ObservableCollection<Location> LocationList
{
    get { return _locationList; };
    set
    {
        if (_locationList == value)
            return;
        _locationList = value;
        OnPropertyChanged();
}

private Location _currentLocation;
public Location CurrentLocation
{
    get { return _currentLocation; };
    set
    {
        if (_currentLocation == value)
            return;
        _currentLocation = value;
        OnPropertyChanged();
}

public IList BindAllLocation()
{
    if (Repository != null) Repository.Dispose();
        Repository = GetInvoiceRepository();
    IList<Location> locationList = Repository.GetLocations(((App)Application.Current).DataContextFactory);
    return locationList;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    foreach (var item in BindAllLocation())
        LocationList.Add(item);
}

在 Xaml 中:

<ComboBox ItemsSource="{Binding *binding to LocationList*}" Width="100" SelectedItem="{Binding CurrentLocation}">
    <ComboBox.ItemTemplate >
        <DataTemplate>
            <StackPanel Orientation="Horizontal" >
                <CheckBox Content="{Binding LocationValue}"  IsChecked="{Binding IsChecked, Mode=TwoWay}" Width="120" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

我不知道 ComboBox 上的 DataContext,所以如果您对此有疑问,请尝试 this

然后您可以使用以下方法对选定的值进行排序:

var result = LocationList.Where(x => x.IsChecked);

当然,你必须有一个 IsChecked 属性。

【讨论】:

    【解决方案2】:

    由于ComboBox 中每个CheckBoxIsChecked 属性都绑定到“全部”CheckBoxIsChecked 属性,因此您应该可以直接从该属性中获取值:

    bool isChecked = all.IsChecked;
    

    另一种选择是将bool 属性添加到Location 类并绑定到该类:

    <CheckBox Content="{Binding LocationValue}"  IsChecked="{Binding IsChecked}" Width="120" />
    

    然后,您可以通过简单地迭代其ItemsSource 来获取ComboBox 中每个CheckBox 的值:

    foreach(var location in LocationFilterComboBox.Items.OfType<Location>())
    {
        bool isChecked = location.IsChecked;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多