Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <ComboBox Name="ddlCountry" IsEditable="True" Text="Multi Select"
- IsTextSearchEnabled="False" StaysOpenOnEdit="True" Width="150" >
- <ComboBox.ItemTemplate>
- <DataTemplate>
- <CheckBox Name="chkCountry" Width="220" Checked="chkCountry_Checked" Unchecked="chkCountry_Unchecked"
- Content="{Binding CountryName}" IsChecked="{Binding Check_Status}" CommandParameter="{Binding CountryId}">
- </CheckBox>
- </DataTemplate>
- </ComboBox.ItemTemplate>
- </ComboBox>
- public partial class MainWindow : Window
- {
- DataTable dtCountry = new DataTable();
- Dictionary<int, string> country = new Dictionary<int, string>();
- public MainWindow()
- {
- InitializeComponent();
- LoadCountry();
- }
- private void LoadCountry()
- {
- try
- {
- using (SqlConnection con = new SqlConnection(Global.constring))
- {
- using (SqlCommand cmd = new SqlCommand("select CountryId, CountryName from Country", con))
- {
- cmd.CommandType = CommandType.Text;
- using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
- {
- sda.Fill(dtCountry);
- }
- }
- }
- dtCountry.Columns.Add("Check_Status", typeof(System.Boolean)); // column added to set IsChecked property of the checkbox
- foreach (DataRow row in dtCountry.Rows)
- {
- //need to set value to NewColumn column
- row["Check_Status"] = false; // or set it to some other value
- }
- dtCountry.AcceptChanges();
- ddlCountry.ItemsSource = null;
- ddlCountry.Items.Clear();
- ddlCountry.ItemsSource = dtCountry.DefaultView;
- ddlCountry.DisplayMemberPath = dtCountry.Columns["CountryName"].ToString();
- ddlCountry.SelectedValuePath = dtCountry.Columns["CountryId"].ToString();
- ddlCountry.SelectedIndex = 0;
- }
- catch (Exception ex)
- {
- }
- }
- private void chkCountry_Checked(object sender, RoutedEventArgs e)
- {
- country.Add(int.Parse(Convert.ToString(((System.Windows.Controls.Primitives.ButtonBase)sender).CommandParameter)),
- Convert.ToString(((System.Windows.Controls.ContentControl)sender).Content));
- ddlCountry.Text = GetCountry();
- }
- private void chkCountry_Unchecked(object sender, RoutedEventArgs e)
- {
- country.Remove(int.Parse(Convert.ToString(((System.Windows.Controls.Primitives.ButtonBase)sender).CommandParameter)));
- ddlCountry.Text = GetCountry();
- }
- //Method to set selected values as text of the combobox
- private string GetCountry()
- {
- StringBuilder cntry = new StringBuilder();
- foreach (var item in cntry)
- {
- cntry.Append(item.Value).Append(",");
- }
- return Convert.ToString(cntry).Trim(',');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement