Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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

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

HTML
<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.
Posted
Updated 19-Jun-16 15:44pm
v2

1 solution

I solved my own problem. I decided to go the array route.

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


C#
public class Items
   {
       public string Col1 { get; set; }
       public string Col2 { get; set; }
   }


C#
				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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900