简体   繁体   中英

CheckComboBox WPF Extended Toolkit

I have a database table named Charges in SQL Server having three columns (ChargeName, Charge, Type). Here is the snapshot of the populated table below:

快照

I am using Extended WPF Toolkit's CheckComboBox Control. I wanted to print the selected items from dropdown.

Here is my XAML code file

<Window x:Class="RTS_Management.TestScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
    Title="TestScreen" Height="300" Width="300">
<Grid>
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <TextBlock Margin="5">Purpose: </TextBlock>
            <xctk:CheckComboBox x:Name="_combo" 
                         HorizontalAlignment="Center" 
                         VerticalAlignment="Center" 
                         DisplayMemberPath="ChargeName"
                         ValueMemberPath="ChargeName"

                          />
            <Button Name="display" 
                    Click="display_Click"
                    Margin="5">
                Display Selected
            </Button>
        </StackPanel>

    </StackPanel>
</Grid>

And this is the Code Behind File

using MessageBox = System.Windows.MessageBox;

namespace RTS_Management
{
    /// <summary>
    /// Interaction logic for TestScreen.xaml
    /// </summary>
    public partial class TestScreen : Window
    {
        bool handle = true;
        public TestScreen()
        {
            InitializeComponent();
            BindTreatmentComboBox(_combo);
        }

        // displaying data in ComboBox
        public void BindTreatmentComboBox(CheckComboBox comboBoxName)
        {
            string ConString = ConfigurationManager.ConnectionStrings["RTS_ManagementModel"].ConnectionString;
            string CmdString = string.Empty;
            SqlConnection conn = new SqlConnection(ConString);
            try
            {
                conn.Open();
                CmdString = "SELECT ChargeName "
                    + "FROM Charges ";
                SqlDataAdapter da = new SqlDataAdapter(CmdString, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Charges");
                comboBoxName.ItemsSource = ds.Tables["Charges"].DefaultView;
                //comboBoxName.ItemsSource = ds;

            }
            catch (Exception ex)
            {

                Xceed.Wpf.Toolkit.MessageBox msg = new Xceed.Wpf.Toolkit.MessageBox();
                msg.ShowMessageBox(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }

        private void display_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(_combo.SelectedValue.ToString());
        }
    }
}

What I am missing? Guys help me I am not good at WPF.

By converting the dataset to a dictionary in the code-behind, I'm able to see the selected values is the pop-up message box with the following code updates:

xaml:

...
<xctk:CheckComboBox x:Name="_combo" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center" 
            DisplayMemberPath="Key"
            ValueMemberPath="Value"
            ItemsSource="{Binding}"
            />
...

Code Behind:

...
comboBoxName.ItemsSource = ds.Tables["Charges"]
                             .AsEnumerable()
                             .ToDictionary<DataRow, string, string>(
                                r => r[0].ToString(), // Key
                                r => r[0].ToString()  // Value
                              );
...

Have you an Id column in your table? If your answer is yes then try something like this:

public void BindTreatmentComboBox(CheckComboBox comboBoxName)
{
    ...
    try
    {
        conn.Open();
        CmdString = "SELECT Id, ChargeName FROM Charges";
        SqlDataAdapter da = new SqlDataAdapter(CmdString, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "Charges");

        var data = ds.Tables["Charges"].DefaultView;

        comboBoxName.DisplayMemberPath = "ChargeName"
        comboBoxName.ValueMemberPath = "Id"
        comboBoxName.ItemsSource = data;    
    }
    ...
}

Xaml:

<xctk:CheckComboBox x:Name="_combo" 
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center"/>

Also put a break point at this line :

        comboBoxName.DisplayMemberPath = "ChargeName"

and check value of data variable. You your table records should be in it.

On the other hand i recommend you to follow MVVM pattern in your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM