简体   繁体   中英

Bind data to TextBox inside DataGridTextColumn.Header

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM