[英]CheckComboBox WPF Extended Toolkit
I have a database table named Charges in SQL Server having three columns (ChargeName, Charge, Type). 我在SQL Server中有一个名为Charges的数据库表,该表具有三列 (ChargeName,Charge,Type)。 Here is the snapshot of the populated table below:
这是下面的填充表的快照:
I am using Extended WPF Toolkit's CheckComboBox Control. 我正在使用扩展WPF工具包的CheckComboBox控件。 I wanted to print the selected items from dropdown.
我想从下拉菜单中打印所选项目。
Here is my XAML code file 这是我的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>
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.
伙计们帮我,我不擅长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: 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? 您的表格中有
Id
栏吗? 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: Xaml:
<xctk:CheckComboBox x:Name="_combo"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
Also put a break point
at this line : 还要在此行上设置一个
break point
:
comboBoxName.DisplayMemberPath = "ChargeName"
and check value of data
variable. 并检查
data
变量的值。 You your table records should be in it. 您的表记录应该在其中。
On the other hand i recommend you to follow MVVM pattern in your project. 另一方面,我建议您在项目中遵循MVVM模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.