Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. GridView CellTemplate

GridView CellTemplate

Scheduled Pinned Locked Moved WPF
wpfcsharpwcfcomdata-structures
7 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • realJSOPR Offline
    realJSOPR Offline
    realJSOP
    wrote on last edited by
    #1

    So I have this ListView, and I want to make a specific column have a different font family, so I tried setting the CellTemplate 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

    L 1 Reply Last reply
    0
    • realJSOPR realJSOP

      So I have this ListView, and I want to make a specific column have a different font family, so I tried setting the CellTemplate 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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      realJSOPR 2 Replies Last reply
      0
      • L Lost User

        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

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • L Lost User

          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

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • realJSOPR realJSOP

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            realJSOPR 1 Reply Last reply
            0
            • L Lost User

              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

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              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

              L 1 Reply Last reply
              0
              • realJSOPR realJSOP

                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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                That makes sense.

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups