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.

7
  • Can you show the viewmodel here. Commented Mar 11, 2016 at 9:55
  • It's pretty huge , so I share it on googleDrive docs.google.com/document/d/… Commented Mar 11, 2016 at 10:11
  • Try to bind with DataContext prefix. Something like this : <DataGridTextColumn Binding="{Binding DataContext.IMEI}">... Commented Mar 11, 2016 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. Commented Mar 11, 2016 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 Commented Mar 11, 2016 at 11:29

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I read linked post and refactor code to this <DataGridTextColumn Binding="{Binding IMEI}"> <DataGridTextColumn.HeaderTemplate> <DataTemplate> <TextBox MinWidth="90" Text="{Binding DataContext.IMEIFilter, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/> </DataTemplate> </DataGridTextColumn.HeaderTemplate> </DataGridTextColumn> and it's work now!
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.