I have a datagrid which is defined at the beginning
DataGrid dtgWatch;
and linked to a Dataset dtsWatch;
dtgWatch.ItemsSource = dtsWatch.Tables[0].DefaultView;
in the middle the dataset is filled with rows with
dtsWatch.Tables[0].Rows.Add(string1, string2);
The purpose is something like the following tab:
| string1_A | string2_A |
| string1_B | string2_B |
| string1_B | string2_C |
that works properly. Now the change: at a point of that reasoning (and only for one row) I have to put an image and not a string. That means:
| string1_A | string2_A |
| string1_B | Image2_B | <---------------
| string1_B | string2_C |
Theoretically speaking this should work (no compilation error) for in case of the image I am adding the line with
Image img =...
dtsWatch.Tables[0].Rows.Add(string1, img);
but the result is
| string1 | System.Windows.Controls.Image |
A possible solution I have thought of is to put a marker somewhere and then at the runtime (Loading row event or something like that) to change the content of the cell with the image. Unfortunately I could do that for I could get to directly access and modify the cell.
I am doing all that with C# wpf with the appdomain tecnique. That means that I have not xaml, I have to do everything in code behind. All what I have is a grid to add with the datagrid.
I have tried that but it is not completed and all the other solutions I found were implying the use of a xaml
Thanks for any help
Patrick
To achieve the desired behaviour you have to create your DataGridColumns manually.
You can still write Xaml, because you can easily parse and create the corresponding objects in code behind. See System.Windows.Markup.XamlReader.Load
So this should work for you
dtgWatch.AutoGenerateColumns = false;
foreach (DataColumn column in dtsWatch.Tables[0].Columns)
{
string dataGridTemplateColumn = $@"
<DataGridTemplateColumn
Header=""{column.ColumnName}""
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl Content=""{{Binding {column.ColumnName}}}"" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>";
XmlReader xr = XmlReader.Create(new StringReader(dataGridTemplateColumn));
dtgWatch.Columns.Add((DataGridTemplateColumn)System.Windows.Markup.XamlReader.Load(xr));
}
OK I have followed a different approach:
private void Dg_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < dg.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
for (int j = 0; j < dg.Columns.Count; j++)
{
TextBlock cellContent = dg.Columns[j].GetCellContent(row) as TextBlock;
var strContent = cellContent.Text;
if (strContent == "BBB2")
{
// This if I want merely change the value (not my case)
//cellContent.Text = "XXXX";
//This if I want to put an image
var dtgCell = cellContent.Parent as DataGridCell;
dtgCell.Content = new Image() { Source = new BitmapImage(new Uri(@"C:\Users\Pictures\Ball_Red.png")) };
}
}
}
}
So instead trying to modify the dtg BEFORE showing it, I change it AFTER.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.