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. Binding converter in code behind

Binding converter in code behind

Scheduled Pinned Locked Moved WPF
questioncsswpfwcftutorial
5 Posts 3 Posters 0 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.
  • U Offline
    U Offline
    USAFHokie80
    wrote on last edited by
    #1

    Hi... So I'm creating a column collection for a data grid in the code behind because I don't know the number of columns needed until run time. I need to bind the background color to a converter but i'm not sure how to do it. The background property is typed as a Brush, but of course the converter is a binding... so how do I get this working? In the code below, the "DataMemberBinding" works because that property is a binding type... So I just need to get the background working. Thanks, Sam.

    Dim c = New Telerik.Windows.Controls.GridViewDataColumn With {.Header = sched.ShipName,
    .IsReadOnly = True,
    .IsFilterable = True,
    .IsSortable = True,
    .Background = New System.Windows.Data.Binding With {.Converter = New BackgroundColorConverter(),
    .ConverterParameter = (s + 1).ToString},
    .IsGroupable = False,
    .ShowFieldFilters = True,
    .ShowDistinctFilters = False,
    .DataMemberBinding = New System.Windows.Data.Binding With {.Converter = New ColumnConverter(),
    .ConverterParameter = (s + 1).ToString}
    }
    columnCollection.Add(c)

    Richard DeemingR M 2 Replies Last reply
    0
    • U USAFHokie80

      Hi... So I'm creating a column collection for a data grid in the code behind because I don't know the number of columns needed until run time. I need to bind the background color to a converter but i'm not sure how to do it. The background property is typed as a Brush, but of course the converter is a binding... so how do I get this working? In the code below, the "DataMemberBinding" works because that property is a binding type... So I just need to get the background working. Thanks, Sam.

      Dim c = New Telerik.Windows.Controls.GridViewDataColumn With {.Header = sched.ShipName,
      .IsReadOnly = True,
      .IsFilterable = True,
      .IsSortable = True,
      .Background = New System.Windows.Data.Binding With {.Converter = New BackgroundColorConverter(),
      .ConverterParameter = (s + 1).ToString},
      .IsGroupable = False,
      .ShowFieldFilters = True,
      .ShowDistinctFilters = False,
      .DataMemberBinding = New System.Windows.Data.Binding With {.Converter = New ColumnConverter(),
      .ConverterParameter = (s + 1).ToString}
      }
      columnCollection.Add(c)

      Richard DeemingR Online
      Richard DeemingR Online
      Richard Deeming
      wrote on last edited by
      #2

      Try something like this:

      Dim column As New GridViewDataColumn With
      {
      .Header = sched.ShipName,
      .IsReadOnly = True,
      .IsFilterable = True,
      .IsSortable = True,
      .IsGroupable = False,
      .ShowFieldFilters = True,
      .ShowDistinctFilters = False,
      .DataMemberBinding = New Binding With
      {
      .Converter = New ColumnConverter(),
      .ConverterParameter = (s + 1).ToString
      }
      }

      Dim backgroundBinding As New Binding With
      {
      .Converter = New BackgroundColorConverter(),
      .ConverterParameter = (s + 1).ToString},
      }

      column.SetBinding(GridViewDataColumn.BackgroundProperty, backgroundBinding)


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      U 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Try something like this:

        Dim column As New GridViewDataColumn With
        {
        .Header = sched.ShipName,
        .IsReadOnly = True,
        .IsFilterable = True,
        .IsSortable = True,
        .IsGroupable = False,
        .ShowFieldFilters = True,
        .ShowDistinctFilters = False,
        .DataMemberBinding = New Binding With
        {
        .Converter = New ColumnConverter(),
        .ConverterParameter = (s + 1).ToString
        }
        }

        Dim backgroundBinding As New Binding With
        {
        .Converter = New BackgroundColorConverter(),
        .ConverterParameter = (s + 1).ToString},
        }

        column.SetBinding(GridViewDataColumn.BackgroundProperty, backgroundBinding)


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        U Offline
        U Offline
        USAFHokie80
        wrote on last edited by
        #3

        Thanks for the reply. SetBinding doesn't exist on the GridViewDataColumn type though... I did figure it out, however:

        Dim c = New Telerik.Windows.Controls.GridViewDataColumn With {.Header = sched.ShipName,
        .IsReadOnly = True,
        .IsFilterable = True,
        .IsSortable = True,
        .IsGroupable = False,
        .ShowFieldFilters = True,
        .ShowDistinctFilters = False,
        .DataMemberBinding = New System.Windows.Data.Binding With {.Converter = New ColumnConverter(),
        .ConverterParameter = (s + 1).ToString}
        }
        Dim backgroundBinding As New System.Windows.Data.Binding With {.Converter = New BackgroundColorConverter,
        .ConverterParameter = (s + 1).ToString}

                    backgroundBinding.Source = c
                    System.Windows.Data.BindingOperations.SetBinding(c, Telerik.Windows.Controls.GridViewDataColumn.BackgroundProperty, backgroundBinding)
        
                    columnCollection.Add(c)
        
        Richard DeemingR 1 Reply Last reply
        0
        • U USAFHokie80

          Thanks for the reply. SetBinding doesn't exist on the GridViewDataColumn type though... I did figure it out, however:

          Dim c = New Telerik.Windows.Controls.GridViewDataColumn With {.Header = sched.ShipName,
          .IsReadOnly = True,
          .IsFilterable = True,
          .IsSortable = True,
          .IsGroupable = False,
          .ShowFieldFilters = True,
          .ShowDistinctFilters = False,
          .DataMemberBinding = New System.Windows.Data.Binding With {.Converter = New ColumnConverter(),
          .ConverterParameter = (s + 1).ToString}
          }
          Dim backgroundBinding As New System.Windows.Data.Binding With {.Converter = New BackgroundColorConverter,
          .ConverterParameter = (s + 1).ToString}

                      backgroundBinding.Source = c
                      System.Windows.Data.BindingOperations.SetBinding(c, Telerik.Windows.Controls.GridViewDataColumn.BackgroundProperty, backgroundBinding)
          
                      columnCollection.Add(c)
          
          Richard DeemingR Online
          Richard DeemingR Online
          Richard Deeming
          wrote on last edited by
          #4

          USAFHokie80 wrote:

          SetBinding doesn't exist on the GridViewDataColumn type

          The BindingOperations.SetBinding method is an extension method. If you import the System.Windows.Data namespace, you'll be able to call it as c.SetBinding(...).


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • U USAFHokie80

            Hi... So I'm creating a column collection for a data grid in the code behind because I don't know the number of columns needed until run time. I need to bind the background color to a converter but i'm not sure how to do it. The background property is typed as a Brush, but of course the converter is a binding... so how do I get this working? In the code below, the "DataMemberBinding" works because that property is a binding type... So I just need to get the background working. Thanks, Sam.

            Dim c = New Telerik.Windows.Controls.GridViewDataColumn With {.Header = sched.ShipName,
            .IsReadOnly = True,
            .IsFilterable = True,
            .IsSortable = True,
            .Background = New System.Windows.Data.Binding With {.Converter = New BackgroundColorConverter(),
            .ConverterParameter = (s + 1).ToString},
            .IsGroupable = False,
            .ShowFieldFilters = True,
            .ShowDistinctFilters = False,
            .DataMemberBinding = New System.Windows.Data.Binding With {.Converter = New ColumnConverter(),
            .ConverterParameter = (s + 1).ToString}
            }
            columnCollection.Add(c)

            M Offline
            M Offline
            mohitsaxena1
            wrote on last edited by
            #5

            Hi, You can use DataGrid_AutoGeneratedColumns() event and set the datagrid.ColumnHeaderStyle, can set background color ,fore-color etc,with help ColumnHeaderStyle Property. I hope that it is useful for you. Thanks Mohit Saxena

            mohit Saxena

            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