2

I have an ObservableCollection:

public ObservableCollection<RepairInfo> RepairList { get; set; }

And I'm binding it to a DataGrid:

<DataGrid SelectionMode="Single" ItemsSource="{Binding RepairList}" Name="RepairsGrid" AutoGenerateColumns="False">

I don't use all properties of my RepairInfo class so I specify which property have to use each columns, like this:

<DataGridTextColumn Binding="{Binding IMEI}">
    <DataGridTextColumn.Header>
        <TextBox MinWidth="90" Text="{Binding IMEIFilter, UpdateSourceTrigger=PropertyChanged}" />
    </DataGridTextColumn.Header>                
</DataGridTextColumn>

The TextBox inside DataGridTextColumn.Header using for filtering data.

So here is my problem - value from TextBox don't update property in ViewModel. If I put this TextBox outside DataGridTextColumn.Header everything is fine. I guess it causes by Binding property of my DataGridTextColumn but I don't know how to resolve it.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
dantey89
  • 2,167
  • 24
  • 37
  • Can you show the viewmodel here. – Gopichandar Mar 11 '16 at 09:55
  • It's pretty huge , so I share it on googleDrive https://docs.google.com/document/d/1wt4AJ49E-cr9du6T7QKgbFaOMoqVnG10WSaJyReMrOs/edit?usp=sharing – dantey89 Mar 11 '16 at 10:11
  • Try to bind with DataContext prefix. Something like this : ... – Marcin Mar 11 '16 at 10:26
  • I feel its not problem with binding or view model. Its more to do with how the header template is defined here. My guess is some kind of data template should be defined to fix this. – Carbine Mar 11 '16 at 10:27
  • @dantey89 Can you tell me, which class the `IMEI` and `IMEIFilter` resides? I will help me to give the solution in more precise way. Thanks – Gopichandar Mar 11 '16 at 11:29
  • @dantey89 Have you tried the solution? – Gopichandar Mar 11 '16 at 11:48
  • ImeiFilter - is a string property in ViewModel wich contains text from the TextBox for future filtering. IMEI - is a property of RepairInfo wich was closed ObservableCollection – dantey89 Mar 11 '16 at 11:54

1 Answers1

2

IMO. . better way would be having the property for the Header inside the MainWindow Viewmodel and you can Bind it like this

 <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding IMEI}"> 
            <DataGridTextColumn.Header>
                <TextBox MinWidth="90" Text="{Binding DataContext.IMEIFilter, 
                           RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
            </DataGridTextColumn.Header>
        </DataGridTextColumn>
  </DataGrid.Columns>

Reference: WPF datagrid header text binding

Community
  • 1
  • 1
Gopichandar
  • 2,742
  • 2
  • 24
  • 54
  • I read linked post and refactor code to this ` ` and it's work now! – dantey89 Mar 11 '16 at 12:26
  • The main thing was to add this code `RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}` to the TextBox so it can undertand where to put value – dantey89 Mar 11 '16 at 12:29