从SQL数据库绑定菜单项 - WPF / C# [英] Menu item binding from SQL database - WPF/C#
问题描述
Hello Developers,
我的WPF / C#菜单项有问题。我想让菜单项从SQL数据库填充。我有以下代码。我可能会错过XAML中的某些内容来将数据从数据库绑定到窗口,但让我们看看你的想法。
C#Snippet
const < span class =code-keyword> string sql = SELECT DISTINCT [PT]。[PhysTypeCode] ,[PT]。[PhysTypeDesc],0 AS [ParentID] FROM [dbo]。[PhysicianTypes] AS [PT] WHERE [PT]。[PhysTypeDesc]<>' - 选择 - 'ORDER BY [PT] 。[PhysTypeDesc] ASC;;
Log.Debug( SP: + sql + for GetPhysTypes());
Log.Debug( 为GetPhysTypes()调用和创建的Dbconn);
var connectionString = Settings.Default.Dbconn;
使用( var conn = new SqlConnection(connectionString))
使用( var cmd = new SqlCommand(sql,conn))
{
try
{
conn 。打开();
使用( var adapter = new SqlDataAdapter(cmd))
{
var ds = new DataSet( );
var dt = new DataTable();
adapter.Fill(ds);
dt = ds.Tables [ 0 ];
var mbar = dt.Select( ParentID = + 0 );
foreach ( var dr in mbar)
{
MiTypeAc.Items.Add( new MenuItem(dr [ PhysTypeDesc]。ToString()));
}
Log.Debug( 为GetPhysTypes()执行命令并完成阅读器跨度>);
}
}
catch (SqlException ex)
{... // 捕获中的更多代码,但这里不重要......
调用GetPhysTypes
private void MiTypeAc_Loaded( object sender,System.Windows.RoutedEventArgs e)
{
GetPhysTypes( );
}
现在我非常讨厌的部分(主要是因为我不完全理解那么好的绑定)
XMAL
< MenuItem 标题 = 按类型 >
< MenuItem x:名称 = MiTypeAc 标题 = AC ItemsSource = {Binding PhysTypeDesc} 已加载 = MiTypeAc_Loaded / >
< / MenuItem >
窗口加载会发生什么,我导航到菜单和菜单打开一个大区域(垂直关于21条记录的样子--21条记录是来自SQL语句的记录数),但它是空的。所以查询有效,C#正在工作(我猜),但数据与窗口的绑定不是......它是空的......
想法?
更新:我从SQL获取数据,因此将数据从C#转换为XAML是一个问题...
调试器:mbar = {System.Data.DataRow [21]}
调试器:mbar [0] .ItemArray [1] =麻醉学助理
我尝试过:
上面的代码。我真的不想对这些值进行硬编码,因为随着时间的推移,用户可以并且会在系统中添加描述和类型。
我解决了我自己的问题。我决定去阵列路线。
< menuitem header = 按类型 x:name = MiTypeAc itemssource = {Binding Col2} 已加载 = MiTypeAc_Loaded xmlns:x = #unknown >
< menuitem.itemcontainer style >
< style >
< setter property =MenuItem.Headervalue ={绑定 Col2 } />
<! - < Setter Property =MenuItem.CommandValue ={ Binding Open < /跨度>}/> - >
< / style >
< / menuitem.itemcontainer 样式 >
< / menuitem >
public class 项目
{
public string Col1 { get ; set ; }
public string Col2 { get 跨度>; set ; }
}
尝试
{
Items [] items = null ;
conn.Open();
使用( var reader = cmd.ExecuteReader())
{
var list = new List< items>();
while (reader.Read())
list.Add( new 项目{Col2 = reader.GetString( 1 )});
items = list.ToArray();
MiTypeAc.ItemsSource = items;
}
Log.Debug( 为GetPhysTypes执行的命令和读者已完成( )跨度>);
}
< / items > 跨度>
Hello Developers,
I have a problem with a WPF/C# menu item. I want to make the menu items populate from a SQL database. I have the following code. I'm likely missing somehting in the XAML to bind the data from the database to the window, but let's see what you think.
C# Snippet
const string sql = "SELECT DISTINCT [PT].[PhysTypeCode], [PT].[PhysTypeDesc], 0 AS [ParentID] FROM [dbo].[PhysicianTypes] AS [PT] WHERE [PT].[PhysTypeDesc] <> '-- Select --' ORDER BY [PT].[PhysTypeDesc] ASC;";
Log.Debug("SP: " + sql + " for GetPhysTypes()");
Log.Debug("Dbconn called and created for GetPhysTypes()");
var connectionString = Settings.Default.Dbconn;
using (var conn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand(sql, conn))
{
try
{
conn.Open();
using (var adapter = new SqlDataAdapter(cmd))
{
var ds = new DataSet();
var dt = new DataTable();
adapter.Fill(ds);
dt = ds.Tables[0];
var mbar = dt.Select("ParentID=" + 0);
foreach (var dr in mbar)
{
MiTypeAc.Items.Add(new MenuItem(dr["PhysTypeDesc"].ToString()));
}
Log.Debug("Command executed and reader completed for GetPhysTypes()");
}
}
catch (SqlException ex)
{...//more code in the catch, but not important here...
Calling GetPhysTypes
private void MiTypeAc_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
GetPhysTypes();
}
Now the part I really hate (mainly because I don't fully understand binding that well)
XMAL
<MenuItem Header="By Type">
<MenuItem x:Name="MiTypeAc" Header="A-C" ItemsSource="{Binding PhysTypeDesc}" Loaded="MiTypeAc_Loaded"/>
</MenuItem>
What happens is the window loads, I navigate to the menu, and the menu opens with a large area (vertical about what 21 records would look like - 21 records being the count of records from the SQL statement), but it's empty. So the query works, the C# is working (i guess), but the binding of data to the window is not... it's empty...
thoughts?
Update: I am getting data from the SQL, so it has to be an issue getting data from C# to XAML...
Debugger: mbar = {System.Data.DataRow[21]}
Debugger: mbar[0].ItemArray[1] = "Anesthesiology Assistant"
What I have tried:
The above code. I really don't want to hard code the values since the users can, and will, add descriptions and types to the system as time goes on.
I solved my own problem. I decided to go the array route.
<menuitem header="By Type" x:name="MiTypeAc" itemssource="{Binding Col2}" loaded="MiTypeAc_Loaded" xmlns:x="#unknown"> <menuitem.itemcontainerstyle> <style> <setter property="MenuItem.Header" value="{Binding Col2}" /> <!--<Setter Property="MenuItem.Command" Value="{Binding Open}"/>--> </style> </menuitem.itemcontainerstyle> </menuitem>
public class Items { public string Col1 { get; set; } public string Col2 { get; set; } }
try { Items[] items = null; conn.Open(); using (var reader = cmd.ExecuteReader()) { var list = new List<items>(); while (reader.Read()) list.Add(new Items { Col2 = reader.GetString(1) }); items = list.ToArray(); MiTypeAc.ItemsSource = items; } Log.Debug("Command executed and reader completed for GetPhysTypes()"); } </items>
这篇关于从SQL数据库绑定菜单项 - WPF / C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!