110,538
社区成员
发帖
与我相关
我的任务
分享
public partial class MulitComboBoxUserControl : UserControl, INotifyPropertyChanged
{
public MulitComboBoxUserControl()
{
InitializeComponent();
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<MultiComboBoxModel>), typeof(MulitComboBoxUserControl),new UIPropertyMetadata(null));
public ObservableCollection<MultiComboBoxModel> ItemsSource
{
get { return (ObservableCollection<MultiComboBoxModel>)GetValue(ItemsSourceProperty); }
set
{
SetValue(ItemsSourceProperty, value);
}
}
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(ObservableCollection<MultiComboBoxModel>), typeof(MulitComboBoxUserControl), new PropertyMetadata(null,null));
public ObservableCollection<MultiComboBoxModel> SelectedItems
{
get { return (ObservableCollection<MultiComboBoxModel>)GetValue(SelectedItemsProperty); }
set
{
SetValue(SelectedItemsProperty, value);
}
}
private string _selectedText;
public string SelectedText
{
get { return _selectedText; }
set
{
_selectedText = value;
this.NotifyPropertyChanged("SelectedText");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
MultiComboBoxModel model = sender as MultiComboBoxModel;
if (model != null)
{
IEnumerable<MultiComboBoxModel> items = ItemsSource.Where(b => b.IsSelected == true);
SelectedItems = new ObservableCollection<MultiComboBoxModel>(items);
StringBuilder builder = new StringBuilder();
foreach (var item in items)
{
builder.Append(item.Title + " ");
}
SelectedText = builder == null ? string.Empty : builder.ToString();
}
}
}
public class MultiComboBoxModel : INotifyPropertyChanged
{
private string _title;
private bool _isSelected;
#region Properties
public string Title
{
get
{
return _title;
}
set
{
_title = value;
NotifyPropertyChanged("Title");
}
}
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
private int _id;
public int Id
{
get { return _id; }
set
{
_id = value;
this.NotifyPropertyChanged("Id");
}
}
private object _tag;
public object Tag
{
get { return _tag; }
set
{
_tag = value;
this.NotifyPropertyChanged("Tag");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<Grid>
<ComboBox Text="{Binding SelectedText}" IsEditable="True" ItemsSource="{Binding ItemsSource}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}" Click="CheckBox_Click"/>
<TextBlock Text="{Binding Title}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
<control:MulitComboBoxUserControl Grid.Row="0" ItemsSource="{Binding ItemsSource}" SelectedItems="{Binding SelectedItems}"></control:MulitComboBoxUserControl>