GridView CellTemplate
-
So I have this
ListView
, and I want to make a specific column have a different font family, so I tried setting theCellTemplate
for the column. Because of the architecture of the app, I have to apply this template in the C# code. I tried : - Creating a resource like so:<DataTemplate x:Key="PAScodeGridCell" > <TextBlock Text="{Binding}" FontFamily="Consolas" /> </DataTemplate>
and was setting it like so:
column.CellTemplate = (DataTemplate)WpfCommon.Globals.XamlReaderLoad("PAScodeGridCell");
It found the template in the resource file, and set it to the
CellTemplate
property, but the font in that column wasn't Consolas. Next, I tried so I tried specifying the template as a string in the c# code like so:readonly string _CELL_TEMPLATE_ = string.Concat( "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\\">",
"<TextBlock ",
"Text=\"{{Binding Path=PAScode}}\" ",
"FontFamily=\"Consolas\" /></DataTemplate>",and set the property like so
column.CellTemplate = (DataTemplate)XamlReader.Load(xaml);
The result was no different. By all rights, both methods should have worked. I inspected the visual tree while the app was running and it's as if the specified template wasn't applied at all. What am I don't wrong?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
So I have this
ListView
, and I want to make a specific column have a different font family, so I tried setting theCellTemplate
for the column. Because of the architecture of the app, I have to apply this template in the C# code. I tried : - Creating a resource like so:<DataTemplate x:Key="PAScodeGridCell" > <TextBlock Text="{Binding}" FontFamily="Consolas" /> </DataTemplate>
and was setting it like so:
column.CellTemplate = (DataTemplate)WpfCommon.Globals.XamlReaderLoad("PAScodeGridCell");
It found the template in the resource file, and set it to the
CellTemplate
property, but the font in that column wasn't Consolas. Next, I tried so I tried specifying the template as a string in the c# code like so:readonly string _CELL_TEMPLATE_ = string.Concat( "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\\">",
"<TextBlock ",
"Text=\"{{Binding Path=PAScode}}\" ",
"FontFamily=\"Consolas\" /></DataTemplate>",and set the property like so
column.CellTemplate = (DataTemplate)XamlReader.Load(xaml);
The result was no different. By all rights, both methods should have worked. I inspected the visual tree while the app was running and it's as if the specified template wasn't applied at all. What am I don't wrong?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013It's a "resource".
column.CellTemplate = yourListView.Resources\[ yourResName \] as DataTemplate;
[FrameworkElement.Resources Property (System.Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.resources?view=netframework-4.6.1&f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.Windows.FrameworkElement.Resources);k(TargetFrameworkMoniker-.NETFramework,Version%253Dv4.6.1);k(DevLang-csharp)%26rd%3Dtrue)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
It's a "resource".
column.CellTemplate = yourListView.Resources\[ yourResName \] as DataTemplate;
[FrameworkElement.Resources Property (System.Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.resources?view=netframework-4.6.1&f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.Windows.FrameworkElement.Resources);k(TargetFrameworkMoniker-.NETFramework,Version%253Dv4.6.1);k(DevLang-csharp)%26rd%3Dtrue)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
I think I posted the wrong snippet for the first attempt. I used FindResource there. No matter which way I tried it, the CellTemplate showed that it was set, but the column did not reflect the settings I was trying to set.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
It's a "resource".
column.CellTemplate = yourListView.Resources\[ yourResName \] as DataTemplate;
[FrameworkElement.Resources Property (System.Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.resources?view=netframework-4.6.1&f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.Windows.FrameworkElement.Resources);k(TargetFrameworkMoniker-.NETFramework,Version%253Dv4.6.1);k(DevLang-csharp)%26rd%3Dtrue)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
I found the problem. This is the code that was creating the GridViewColumn:
DataTemplate cellTemplate = // code to create cell template, which binds to the itemsource property
Binding binding = new Binding()...
GridViewColumn column = new GridViewColumn()
{
Header = "...",
DisplayMemberBinding = binding,
CellTemplate = cellTemplate,
};I had to change it to NOT set the
DisplayMemberBinding
property:GridViewColumn column = new GridViewColumn()
{
Header = "...",
DisplayMemberBinding = (celltable == null) ? binding : null,
CellTemplate = cellTemplate,
};And now it does exactly what I want it to do. :)
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
I found the problem. This is the code that was creating the GridViewColumn:
DataTemplate cellTemplate = // code to create cell template, which binds to the itemsource property
Binding binding = new Binding()...
GridViewColumn column = new GridViewColumn()
{
Header = "...",
DisplayMemberBinding = binding,
CellTemplate = cellTemplate,
};I had to change it to NOT set the
DisplayMemberBinding
property:GridViewColumn column = new GridViewColumn()
{
Header = "...",
DisplayMemberBinding = (celltable == null) ? binding : null,
CellTemplate = cellTemplate,
};And now it does exactly what I want it to do. :)
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013Glad it's working. Might be a timing thing:
Quote:
Resources can also be referenced by code from within the collection, but be aware that resources created in XAML will definitely not be accessible until after Loaded is raised by the element that declares the dictionary.
(I see now you're "creating" on the fly; I was "switching" when I looked back at my app).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
Glad it's working. Might be a timing thing:
Quote:
Resources can also be referenced by code from within the collection, but be aware that resources created in XAML will definitely not be accessible until after Loaded is raised by the element that declares the dictionary.
(I see now you're "creating" on the fly; I was "switching" when I looked back at my app).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
It's a binding thing - I was using DisplayMemberBinding, and then trying to apply a template that did its own binding - you can't do both, and when you try, it always does the DisplayMemberBinding first.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
It's a binding thing - I was using DisplayMemberBinding, and then trying to apply a template that did its own binding - you can't do both, and when you try, it always does the DisplayMemberBinding first.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013