Мне нужно было создать пользовательский комбинированный список флажков, чтобы представить список календарных месяцев для выбора пользователем.Это приложение позволит пользователю выбирать месяцы, и всякий раз, когда месяц выбран или не выбран, список обновляется.Я перебираю наблюдаемую коллекцию, которая является источником элементов списка, чтобы определить, какие месяцы были проверены.Вместо того, чтобы вручную перебирать наблюдаемую коллекцию, я мог бы, вероятно, использовать LINQ для запроса коллекции, но это на другой день.Если вы хотите проверить это, просто создайте новое приложение wpf под названием CustomComboBox (в 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);
}
}
}
}
}