Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
540
XamGrid TemplateColumn with Custom Control
posted

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());

_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);

break;

default:

break;

}

}

 

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

 

}

}

Parents
  • 138253
    Offline posted

    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.

    XamGridTemplateColumnWithCustomControl.zip
Reply Children