2016-09-06 190 views
0

我有一個名爲數據庫表三列(ChargeName,充電,型號)收費在SQL Server中。 這裏是低於填充表格的快照:CheckComboBox WPF擴展工具包

Snapshot

我使用擴展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。

回答

0

通過在數據集轉換爲字典代碼隱藏,我能看到所選的值是用下面的代碼更新彈出消息框:

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 
          ); 
... 
0

有你在表中的列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模式。

相關問題