Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Multi Select Combobox in Silverlight With C#
WhatsApp
Piyush Pansuriya
Sep 05
2014
4.7
k
0
0
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
Banthia_Mgt_System.Assets.Wrapper_Class;
using
System.Collections.ObjectModel;
using
Banthia_Mgt_System.Classes;
using
Banthia_Mgt_System.Assets.Classes;
namespace
Banthia_Mgt_System.Assets.Multiselect_Combo
{
public
partial
class
UC_Multi_Dept_Combo : UserControl
{
List<CLS_Multi_Dept_Checked_WrapperBE> _Coll_Wrapper_BE;
private
List<CLS_Multi_Dept_Checked_WrapperBE> Coll_Wrapper_BE
{
get
{
return
_Coll_Wrapper_BE;
}
set
{
_Coll_Wrapper_BE = value;
foreach
(var item
in
Coll_Wrapper_BE)
item.PropertyChanged +=
new
System.ComponentModel.PropertyChangedEventHandler(item_PropertyChanged);
cmbDepartment.ItemsSource = value;
if
(cmbDepartment.ItemsSource !=
null
)
SetString();
}
}
public
UC_Multi_Dept_Combo()
{
InitializeComponent();
cmbDepartment.SelectionChanged +=
new
SelectionChangedEventHandler(cmbDepartment_SelectionChanged);
cmbDepartment.DropDownOpened +=
new
EventHandler(cmbDepartment_DropDownOpened);
}
void
cmbDepartment_DropDownOpened(
object
sender, EventArgs e)
{
if
(Coll_Wrapper_BE ==
null
)
Coll_Wrapper_BE = CLS_Multi_Dept_Checked_Wrapper.Get_Wrapper_Coll(
new
List<
int
>());
}
public
static
readonly
DependencyProperty Str_Department_IdsProperty = DependencyProperty.Register(
"Str_Department_Ids"
,
typeof
(
string
),
typeof
(UC_Multi_Dept_Combo),
new
PropertyMetadata(
null
, OnPropertyChangedCallback));
public
string
Str_Department_Ids
{
get
{
return
(
string
)GetValue(Str_Department_IdsProperty);
}
set
{
SetValue(Str_Department_IdsProperty, value);
}
}
private
static
void
OnPropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var obj = (UC_Multi_Dept_Combo)o;
obj.Coll_Wrapper_BE = CLS_Multi_Dept_Checked_Wrapper.Get_Wrapper_Coll(Globle_Function.Get_Coll_From_Str_CSV(obj.Str_Department_Ids));
}
void
item_PropertyChanged(
object
sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if
(e.PropertyName ==
"IS_CHECKED"
)
{
SetString();
//Department_id_String();
}
}
public
void
SetString()
{
string
str_Department_Name =
""
;
string
str_Department_Ids =
""
;
foreach
(var item
in
Coll_Wrapper_BE)
{
if
(item.IS_CHECKED)
{
if
(str_Department_Name !=
""
) str_Department_Name +=
", "
;
if
(str_Department_Ids !=
""
) str_Department_Ids +=
", "
;
str_Department_Name += item.DEPT_MST.DEPARTMENT_NAME;
str_Department_Ids += item.DEPT_MST.DEPARTMENT_ID.ToString();
}
}
SetValue(Str_Department_IdsProperty, str_Department_Ids);
tbDepartment.Text = str_Department_Name;
var Tool =
new
ToolTip() { Content = str_Department_Name };
ToolTipService.SetToolTip(tbDepartment, Tool);
ToolTipService.SetToolTip(cmbDepartment, Tool);
}
void
cmbDepartment_SelectionChanged(
object
sender, SelectionChangedEventArgs e)
{
cmbDepartment.SelectedItem =
null
;
}
private
void
tbDepartment_MouseLeftButtonDown(
object
sender, MouseButtonEventArgs e)
{
cmbDepartment.IsDropDownOpen =
true
;
}
}
}
XAML Code
<
UserControl
x:Class
=
"Banthia_Mgt_System.Assets.Multiselect_Combo.UC_Multi_Dept_Combo"
xmlns
=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
=
"http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
=
"http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
=
"http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
=
"d"
d:DesignWidth
=
"300"
>
<
Grid
x:Name
=
"LayoutRoot"
Background
=
"White"
>
<
ComboBox
Name
=
"cmbDepartment"
Height
=
"23"
HorizontalAlignment
=
"Stretch"
>
<
ComboBox.ItemTemplate
>
<
DataTemplate
>
<
CheckBox
Content
=
"{Binding DEPT_MST.DEPARTMENT_NAME}"
IsChecked
=
"{Binding IS_CHECKED,Mode=TwoWay}"
/>
</
DataTemplate
>
</
ComboBox.ItemTemplate
>
</
ComboBox
>
<
TextBlock
x:Name
=
"tbDepartment"
TextTrimming
=
"WordEllipsis"
MouseLeftButtonDown
=
"tbDepartment_MouseLeftButtonDown"
HorizontalAlignment
=
"Stretch"
VerticalAlignment
=
"Center"
MinHeight
=
"15"
Text
=
""
Margin
=
"5,0,20,0"
/>
</
Grid
>
</
UserControl
>
Wrapper Class
using
System;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Ink;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
Banthia_Mgt_System.ServiceReference1;
using
System.Collections.Generic;
using
System.Collections.ObjectModel;
using
Banthia_Mgt_System.Classes;
using
System.ComponentModel;
namespace
Banthia_Mgt_System.Assets.Wrapper_Class
{
public
class
CLS_Multi_Dept_Checked_WrapperBE : INotifyPropertyChanged
{
private
bool
_IS_CHECKED;
public
bool
IS_CHECKED
{
get
{
return
_IS_CHECKED; }
set
{
_IS_CHECKED = value;
OnPropertyChanged(
"IS_CHECKED"
);
}
}
public
DEPARTMENT_MASTER DEPT_MST {
get
;
set
; }
#region INotifyPropertyChanged Members
public
event
PropertyChangedEventHandler PropertyChanged;
private
void
OnPropertyChanged(
string
propertyName)
{
if
(
this
.PropertyChanged !=
null
)
{
this
.PropertyChanged(
this
,
new
PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
public
static
class
CLS_Multi_Dept_Checked_Wrapper
{
public
static
List<CLS_Multi_Dept_Checked_WrapperBE> Get_Wrapper_Coll(List<
int
> Coll_Dept_ID)
{
var CollBE =
new
List<CLS_Multi_Dept_Checked_WrapperBE>();
foreach
(var item
in
GlobalClass.GColl_DeptMaster)
{
var obj =
new
CLS_Multi_Dept_Checked_WrapperBE()
{
IS_CHECKED = (Coll_Dept_ID.Where(ar => ar == item.DEPARTMENT_ID).Count() > 0),
DEPT_MST = item
};
CollBE.Add(obj);
}
return
CollBE;
}
public
static
List<
int
> Get_Object_BE_Coll(List<CLS_Multi_Dept_Checked_WrapperBE> Coll)
{
List<
int
> NewColl =
new
List<
int
>();
if
(Coll !=
null
)
NewColl =
new
List<
int
>(Coll.Where(ar => ar.IS_CHECKED ==
true
).Select(ar => ar.DEPT_MST.DEPARTMENT_ID));
return
NewColl;
}
}
}
Silverlight
Combobox in Silverlight