简体   繁体   中英

WPF datagrid header not updated

I have the following datagrid :

    <DataGrid ItemsSource="{Binding Clients}" AutoGenerateColumns="False" x:Name="control" HeadersVisibility="Column">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Id}" Header="{Binding DataContext.Localization.Id, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}" />
            <DataGridTextColumn Binding="{Binding Name}" Header="{Binding DataContext.Localization.Name, ElementName=control}" />
        </DataGrid.Columns>
    </DataGrid>

In my ViewModel I have a localization object which contains all the strings of the ui loaded from the resx:

public class ClientListLocalization : LocalizationBase
{
    private string _id;
    private string _name;

    public string Id
    {
        get { return _id; }
        set { _id = value; RaisePropertyChanged(); }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; RaisePropertyChanged(); }
    }

    internal override void Localize()
    {
        Id = Resources.clientview_id;
        Name = Resources.clientview_name;
    }
}

And my view model (simplified)

public class ClientListViewModel
{
    public new ClientListLocalization Localization { get; set; }
}

But in my application, headers are not updated, both with the first way (using RelativeSource ) or the second way (using ElementName )

Any clues ?

This solution fixed my problem: How to bind column header to property in ViewModel? (WPF MVVM)

The column definitions of the DataGrid don't inherit the DataContext , because they're not part of the visual tree, so you can't bind directly to the ViewModel .

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