1

I'm trying to build a DataGrid layout where the first column's name will be dinamically changed. How can I do, into DataGridTextColumn's Header property, to change that? I've saw some examples than the Header property is connected into a StaticResource, but a StaticResource is a fixed value, and that's doesn't work for me, once what I need is several values. Example:

  • If a user select a RadioButton, filtering by hour, the header will be X
  • If filters by day, header will be Y
  • If Filters by month, header will be Z
  • ...

    Remembering, this is one of several examples than i would need to change. Thanks.

    Frank
    • 3,029
    • 5
    • 34
    • 43
    Gustavo Gonçalves
    • 528
    • 1
    • 12
    • 33

    1 Answers1

    1

    This can be done easily with Databinding.

    The CodeBehind Way

    Create a property in the codebehind of your window to hold the string value; I will call mine TextProp. I will assume the elementname of your window is "Window" for this example. In the DataGridTextColumn tag, databind the Header attribute to that property.

    <DataGridTextColumn Header="{Binding TextProp, ElementName=Window}"/>
    

    The MVVM Way

    Do the same as above, except put the property on your viewmodel to which the datagrid is bound. Change the XAML to:

    <DataGridTextColumn Header="{Binding TextProp}"/>
    

    Then all you have to do is change that Property value in whatever way you choose. To get this to update the value when the property changes, you will need to implement INotifyPropertyChanged (Check at the bottom of that post).

    Community
    • 1
    • 1
    CodeWarrior
    • 7,388
    • 7
    • 51
    • 78
    • Thanks for the answer. I did the testes, and it don't work, when I set the binding into the header, the header return null, a white field. I've already implemented INotifyPropertyChanged. I'm using Page instead of Window in this project. Any other suggestions? Thanks in advance. – Gustavo Gonçalves Sep 21 '11 at 17:38
    • Is your page named "Page"? In your ElementaName portion of the binding above, ensure that value is the same as the value in x:Name in the root tag (Page). Also, keep in mind that if you dont give the property an initial value, it will be an empty string in the header... – CodeWarrior Sep 21 '11 at 18:07
    • I'm using a object type Page (WPF). My page's name is pgFornos. In that case, ive used: MyTest property has a initial value. Even But even in this scenario, the header is not changed. It is always blank. – Gustavo Gonçalves Sep 21 '11 at 20:00
    • And the MyTest property is in the same ViewModel as the CounterDtm property? Are you getting any binding errors in your output window? – CodeWarrior Sep 21 '11 at 21:46
    • All those two propertys are in ViewModel. The values ​​for CounterDtm are updated correctly and are presented in the Grid. The property has the values ​​that MyTest would like to see displayed.Only the header doesn't show any value. I don't get any error , as i've said, all the propertys have values. Thanks again. – Gustavo Gonçalves Sep 22 '11 at 12:23
    • Also, the Headers text property, does it have an initial value, or are you setting it in the constructor or afterward? If it does not have an initial value, have you called your PropertyChanged Event? – CodeWarrior Sep 22 '11 at 14:16