I'm trying to bind header of a table to property in my ViewModel.
<Window x:Class="MyProject.Views.Dialogs.ItemsDialog"
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"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
Height="300" Width="600" MinWidth="200" MinHeight="200">
<Window.Resources>
<local:ViewModelLocator x:Key="Locator" />
</Window.Resources>
<Window.DataContext>
<Binding Source="{StaticResource Locator}" Path="ItemsDialogModel" />
</Window.DataContext>
<Window.Title>
<Binding Path="Strings.ItemsDialogTitle" />
</Window.Title>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="{Binding Strings.ItemNameHeader}" />
<DataGridTextColumn Binding="{Binding Name}" Header="{Binding Path=DataContext.Strings.ItemNameHeader, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
// ...
</DataGrid>
<TextBlock Text="{Binding Strings.ItemsNameHeader}" Grid.Row="2" />
</Grid>
</Window>
Window title binding and other bindings outside DataGrid
work fine. Data in grid binds successfully too. For example, TextBlock
under DataGrid
works without any problems. But both ways of header binding in the code above don't work.
How can I bind header of DataGridTextColumn
to Strings.ItemNameHeader
property of view model? Should I use RelativeSource or not?