2017-08-28 88 views
0

我有一个组合框,复选框存在于组合框内。我想要多选复选框的值。我的代码:如何获取组合框WPF内的复选框的值?

<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(); 
} 
+1

我不明白你的问题。您已经在使用绑定。你的价值应该在属性'IsChecked'假设你的绑定;) –

+0

一个共享的代码示例 我怎样才能检索被检查的项目@MightyBadaboom –

+0

@AkhilJain请发布代码,我们可以使用的示例。 – lightlike

回答

0

我不会直接设置的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> 

我不知道在DataContext的组合框,所以如果你有试图this问题。

然后你就可以使用理清选择的值:

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

当然,你必须有一个物业器isChecked为。

0

由于每个CheckBoxComboBoxIsChecked属性绑定到的“所有” CheckBoxIsChecked属性,你应该能够直接从这个获得的价值:

bool isChecked = all.IsChecked; 

另一种选择本来是一个bool属性添加到Location类,并绑定到这一个:

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

然后,您可以得到通过简单地遍历其ItemsSourceComboBox每一个人CheckBox的价值:

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