Hello,
I have a custom control, that is just a class file called cEmployeeItem. This control's base is the textbox control. I want to be able to show this control in the xam grid column, I have tried setting a template column with a data template and a cEmployeeItem inside of that, but the column just shows an empty version of the control.
I am loading the grid with an ObservableCollection datasource and the template column is supposed to be populated from the Employee property which is a cEmployeeItem type. The property is loaded correctly with all the data but the template column just shows an empty version of the control, help!
Below is my code for the cEmployeeItem:
public class cEmployeeItem : TextBox
{
private ASAP.BusinessEntities.Employment _emp;
private ToolTip _tt;
public static readonly DependencyProperty EmployeeItemProperty = DependencyProperty.Register(
"EmployeeItemObject", typeof(cEmployeeItem), typeof(cEmployeeItem), null);
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(cEmployeeItem), null);
public cEmployeeItem()
this._tt = new ToolTip();
this._emp = null;
this.IsReadOnly = true;
}
public cEmployeeItem(ASAP.BusinessEntities.Employment pEmp) : this()
this._emp = pEmp;
SetText();
SetToolTip();
this.SetValue(EmployeeItemProperty, this);
public cEmployeeItem EmployeeItemObject
get { return (cEmployeeItem)GetValue(EmployeeItemProperty); }
set { SetValue(EmployeeItemProperty, value); }
public string Text
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
private void SetText()
if (this._emp != null)
this.Text = string.Format("{0}, {1} {2} {3}", _emp.LastName, _emp.FirstName, _emp.Mid, _emp.Suffix);
else
this.Text = DateTime.Now.ToString();
private void SetToolTip()
Grid _ttGrid = new Grid();
_ttGrid.ColumnDefinitions.Add(new ColumnDefinition());
for (int i = 0; i < 5; i++)
_ttGrid.RowDefinitions.Add(new RowDefinition());
switch (i)
case 0:
TextBlock tblk0A = new TextBlock();
tblk0A.Text = "ID:";
tblk0A.SetValue(Grid.ColumnProperty, 0);
tblk0A.SetValue(Grid.RowProperty, i);
_ttGrid.Children.Add(tblk0A);
TextBlock tblk0B = new TextBlock();
tblk0B.Text = "999999";
tblk0B.SetValue(Grid.ColumnProperty, 1);
tblk0B.SetValue(Grid.RowProperty, i);
_ttGrid.Children.Add(tblk0B);
break;
case 1:
TextBlock tblk1A = new TextBlock();
tblk1A.Text = "RANK:";
tblk1A.SetValue(Grid.ColumnProperty, 0);
tblk1A.SetValue(Grid.RowProperty, i);
_ttGrid.Children.Add(tblk1A);
TextBlock tblk1B = new TextBlock();
tblk1B.Text = "TEST RANK";
tblk1B.SetValue(Grid.ColumnProperty, 1);
tblk1B.SetValue(Grid.RowProperty, i);
_ttGrid.Children.Add(tblk1B);
default:
this._tt.Content = _ttGrid;
ToolTipService.SetToolTip(this, this._tt);
protected override void OnTap(GestureEventArgs e)
base.OnTap(e);
protected override void OnMouseEnter(MouseEventArgs e)
base.OnMouseEnter(e);
Hello Jason,
Thank you for your post. I have been looking into it and I created a sample project for you with the functionality you want. Basically I bound the Employee’s Text Property to the underlying object’s Property I want to show. Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Stefan,
Thank you for your sample project. I downloaded and viewed the code. It seems fairly similiar to what I am trying to do. I replaced my xaml with yours from the project you provided and made some changes. For the the datasource for the grid I am using a mBidTemplateRecords object which is an ObservableCollection<mBidTemplateRecordParent>. The mBidTemplateRecordParent has 3 properties, Organization, Assignment Name and Children (which is an ObservableCollection<mBidTemplateRecordChild>. The mBidTemplateRecordChild has several properties of int and string, includin the Employee property which is of cEmployeeItem (which is the object I am trying to show in the column).
Now, when I run the program, the datasource object, mBidTemplateRecords, gets populated correctly from the available data, includin the cEmployeeItem property in the children. When the view model property notifies the view that the datasource has been updated, it loads everything correctly into the grid, except for the cEmployeeItem column, which just shows like an empty txtbox. There is definitely an empty instance of cEmployeeItem, but it didn't get populated. I setup up a SelectedRow changed event and added a break point and then selected a row. When I look at the cells of the selected child row, it does show that there is a valid instance of the cEmployeeItem with all the properties filled correctly from the datasource,so this leaves me quite confused.
My code gets a bit complex, so instead of uploading that here I have modified the project you provided me with what I am trying to achieve. I commented out the xaml part because I am now sure how to approach it. I hope you can help me out here, it feels like I am missing something simple.
XAML for Grid:
<
Grid>
C# Code for MainWindow.xaml.cs
public
partial class MainWindow : Window
DataTable table1 = new DataTable("patients");
public MainWindow()
InitializeComponent();
table1.Columns.Add(
"name");
"id");
table1.Rows.Add(
new Employee("sam","1"), 1);
new Employee("mark", "2"), 2);
new Employee("joe","3"), 3);
DataSet set = new DataSet("office");
set.Tables.Add(table1);
this.DataContext = set.Tables[0].DefaultView;
public class Employee : TextBox
public Employee()
public Employee(string pName, string pID) : this()
this.Text = pName;
ToolTipService.SetToolTip(this, pID);
public Employee ThisObject { get; set; }