如何获取放在wpf中的datagrid列标题中的文本框文本

时间:2015-10-24 10:43:40

标签: wpf

我希望在每个colunmns标头中都有一个带有文本框的数据网格来过滤datagrid。 我试过阅读thisthis。这是我的数据网格:

 <DataGrid  x:Name="CustomersDataGrid" BorderThickness="1" 
              AutoGenerateColumns="False" 
              CanUserAddRows="False" 
              CanUserResizeColumns="True" 
              CanUserResizeRows="False" 
              CanUserDeleteRows="False" 
              SelectionMode="Single" 
              SelectionUnit="FullRow" 
              DataContext="{Binding ElementName=UI}"
              ItemsSource="{Binding}"
              IsReadOnly="True" MouseDoubleClick="CustomersDataGrid_MouseDoubleClick">
    <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Customer.Name}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label VerticalAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" Content="Name" Style="{StaticResource GridTitleLabel}"/>
                            <TextBox Name="NameTextBox" MinWidth="100" TextChanged="SearchTextBox_TextChanged" Text="{Binding FilterString, ElementName=UI, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>
    </DataGrid.Columns>

这是codebehind中的filterstring:

private string _filterString;
    public string FilterString
    {
        get { return _filterString; }
        set
        {
            _filterString = value;
            NotifyPropertyChanged("FilterString");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

但是无法获取textBox文本,它没有正确绑定且filterstring值始终为null

2 个答案:

答案 0 :(得分:1)

据我了解,您没有在Window上实现INotifyPropertyChanged。 让我向你展示一个小例子: XAML:

<Window x:Class="FilterSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Name="window"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn>
                    <DataGridTextColumn.HeaderTemplate>
                        <DataTemplate>
                            <Grid Width="120">
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Label Grid.Row="0" 
                                       Content="Some header" 
                                       HorizontalAlignment="Center"/>
                                <TextBox Grid.Row="1"
                                         Text="{Binding Filter, ElementName=window, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>
                            </Grid>
                        </DataTemplate>
                    </DataGridTextColumn.HeaderTemplate>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

代码隐藏:

public partial class MainWindow : INotifyPropertyChanged
{
    private string _filter;

    public MainWindow()
    {
        InitializeComponent();
    }

    public string Filter
    {
        get { return _filter; }
        set
        {
            if (_filter == value) return;
            _filter = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

答案 1 :(得分:0)

当您编写“Binding”时,属性“Filter”必须位于DataContext中。 但是,文本框无法直接访问DataContext,为什么在GridView中。

所以,做这样的事情:

<TextBox Text="{Binding DataContext.Filter, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>