I'm trying to bind data to some sort of custom dependency property or attached property in WPF. I've used the following question and answer- How to create Custom Property for WPF DataGridTextColumn as a frame of reference but I need to bind to the 'tag' in WPF. We can call it whatever, but I call it a tag for simplicity as I'm using it on some checkboxes and textboxes in other parts of the code. My Class is as follows:
public class TagTextColumns : DependencyObject
{
public static readonly DependencyProperty TagProperty = DependencyProperty.RegisterAttached(
"Tag",
typeof(object),
typeof(DataGridColumn),
new FrameworkPropertyMetadata(null));
public static object GetTag(DependencyObject dependencyObject)
{
return dependencyObject.GetValue(TagProperty);
}
public static void SetTag(DependencyObject dependencyObject, object value)
{
dependencyObject.SetValue(TagProperty, value);
}
}
I'd like to setup my DataGridTextColumn in WPF to something similar to the following:
<DataGridTextColumn Binding="{Binding Customer}" Header="Customer" local:TagTextColumns.Tag="{Binding tkey}"/>
where tkey is a reference in the db. I do all this to calculate subtotals in the last column per row. Right now, I use a DataGridTemplateColumn
with a TextBox
inside so I can Tag
it with tkey
I've also read up on TechNet about these things but there seems to be little pointing to what exactly I want to do here. I'm not even sure it's possible, but it would seem like it would be.
EDIT:
This is the code I'm using to attempt to call the tag on CellEditEnding
firing
private void resultsDg_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
MessageBox.Show(TagTextColumns.GetTag(sender as DataGridTextColumn).ToString());
}
More details about the problem at hand:
I am trying on four fields from the database to dynamically calculate the subtotal for the last column as the user makes changes. I was trying to use Linq to SQL for this, but using System.Data.SqlClient
and System.Data
was much faster. Maybe someone can show me a better way. The calculation code is below:
private void CalculateAscessorials(string tkey)
{
decimal SplitTerm = Properties.Settings.Default.SplitTerminal;
decimal SplitDrop = Properties.Settings.Default.SplitDrop;
decimal SiteSplit = Properties.Settings.Default.OnSiteSplit;
decimal Trainer = Properties.Settings.Default.TrainerLoad;
decimal WaitTime = Properties.Settings.Default.TerminalWait;
decimal PumpOut = Properties.Settings.Default.PumpOut;
DataRow[] row = resultDetail.Select("tkey = " + tkey);
decimal payRate;
decimal tempLineItem;
Decimal.TryParse(row[0]["PayForLine"].ToString(), out tempLineItem);
Decimal.TryParse(row[0]["PayRate"].ToString(), out payRate);
if (payRate == 0 && tempLineItem > 0) //this change affts if the rate is 0, that is no rate defined, the rate entered becomes the pay rate for that line only for calculations
row[0]["PayRate"] = tempLineItem;
if (!Convert.ToBoolean(row[0]["SplitDrop"]))
{
Decimal.TryParse(row[0]["PayRate"].ToString(), out payRate);
}
else if (Convert.ToBoolean(row[0]["SplitDrop"]))
{
payRate = SplitDrop;
}
//decimal linePay;
// Decimal.TryParse(row[0]["PayForLine"].ToString(), out linePay);
int terms;
Int32.TryParse(row[0]["SplitLoad"].ToString(), out terms);
decimal waits;
Decimal.TryParse(row[0]["WaitTime"].ToString(), out waits);
int pumps;
Int32.TryParse(row[0]["PumpOut"].ToString(), out pumps);
int sites;
Int32.TryParse(row[0]["SiteSplit"].ToString(), out sites);
int trainings;
Int32.TryParse(row[0]["Trainer"].ToString(), out trainings);
row[0]["PayForLine"] =
(SplitTerm * terms)
+ (waits * WaitTime)
+ (pumps * PumpOut)
+ (sites * SiteSplit)
+ (trainings * Trainer)
+ payRate;
}