Emmanuel Istace
Emmanuel Istace

Reputation: 1249

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 ?

Upvotes: 1

Views: 1145

Answers (1)

Emmanuel Istace
Emmanuel Istace

Reputation: 1249

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.

Upvotes: 2

Related Questions