【问题标题】:CheckComboBox WPF Extended ToolkitCheckComboBox WPF 扩展工具包
【发布时间】:2016-09-06 21:26:52
【问题描述】:

我在 SQL Server 中有一个名为 Charges数据库表,它有 三列(ChargeName、Charge、Type)。 下面是填充表格的快照:

我正在使用扩展 WPF 工具包的 CheckComboBox 控件。我想打印从下拉列表中选择的项目。

这是我的 XAML 代码文件

<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>

这是文件背后的代码

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());
        }
    }
}

我错过了什么?伙计们帮助我,我不擅长 WPF。

【问题讨论】:

    标签: c# wpf xaml wpf-extended-toolkit


    【解决方案1】:

    通过在代码隐藏中将数据集转换为字典,我可以看到所选值是弹出消息框,其中包含以下代码更新:

    xaml:

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

    代码背后:

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

    【讨论】:

      【解决方案2】:

      您的表中有Id 列吗?如果你的答案是肯定的,那么试试这样的:

      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"/>
      

      还要在此行添加break point

              comboBoxName.DisplayMemberPath = "ChargeName"
      

      并检查data 变量的值。你的表记录应该在里面。

      另一方面,我建议您在项目中遵循MVVM 模式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        相关资源
        最近更新 更多