0

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?

Aleksei Petrov
  • 936
  • 12
  • 31
  • I'm trying right now. All google link give `RelativeSource` answer. But It doesn't work, as I mentioned. I tried to use RelativeSource in TextBlock and `x:Type Window` don't work and `x:Type Grid` works! Unfortunately, it don't inside `DataGrid`... – Aleksei Petrov Aug 17 '17 at 08:07
  • https://stackoverflow.com/questions/1658397/wpf-datagrid-header-text-binding – FoggyFinder Aug 17 '17 at 08:07
  • 1
    https://stackoverflow.com/questions/16653005/wpf-datagrid-view-header-binding – FoggyFinder Aug 17 '17 at 08:07
  • if these links gives answer to your Q just mark Q as duplicate. If not - let me know. – FoggyFinder Aug 17 '17 at 08:13
  • Thaks a lot. Didn't thought this is specifically header problem and was googling "accees to window datacontext" and etc... ) – Aleksei Petrov Aug 17 '17 at 08:15
  • glad to help. Just a note. If you have any Q about WPF you can ask about it in the [WPF chat room](https://chat.stackoverflow.com/rooms/18165/wpf) or if you prefer use you native language [C# chat room on Ru-SO](https://chat.stackexchange.com/rooms/26962/c-net--) – FoggyFinder Aug 17 '17 at 08:20

0 Answers0