Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I would like to change my DataGrid column headings have two text boxes in itself. I created a class and Depndency property: myProperty but binding not work. I tried different combinations, unfortunately I do not know where I'm doing a mistake. Please help

What I have tried:

Column:
XML
<extensions:CustomDataGridTextColumn HeaderStyle="{StaticResource Nagłówek2tb}" Header="Nagłówek 1" MyProperty ="Nagłówek2"/>

Style:
<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="Nagłówek2tb">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate >
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="{Binding}" TextWrapping="Wrap"/>
                            <TextBlock Grid.Row="1" Text="{Binding MyProperty, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridTextColumn}}"/>
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Class

C#
public class CustomDataGridTextColumn : System.Windows.Controls.DataGridTextColumn
    {
 
        public string MyProperty
        {
            get { return (string)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }
 
        public static readonly DependencyProperty MyPropertyProperty =
          DependencyProperty.Register("MyProperty", typeof(string), typeof(CustomDataGridTextColumn), new UIPropertyMetadata(""));
 
    }
Posted
Updated 19-Feb-17 22:49pm
Comments
Graeme_Grant 19-Feb-17 10:59am    
what error does the debug output window in VS give?
Rakon22 19-Feb-17 11:14am    
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGridTextColumn', AncestorLevel='1''. BindingExpression:Path=MyProperty; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
Graeme_Grant 19-Feb-17 11:34am    
I've addressed the second problem but the first problem, the binding error relates to:
TextBlock Grid.Row="1" Text="{Binding MyProperty, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridTextColumn}}"/>

What are you tring to bind to?
Rakon22 19-Feb-17 11:56am    
I have seen this topic but when I use DataGridTemplateColumn then DataGrid it takes a lot in Ram memory (200 rows 300 MB Ram) so I am looking for other solutions

DataGridColumnHeader is a terrible beast, it is neither part of the Visual Tree nor Logical Tree. You need a BindingProxy, and a lot of work around. See e.g.
[WPF] How to bind to data when the DataContext is not inherited » Thomas Levesque's .NET blog[^]
 
Share this answer
 
Short answer: {Binding} is not a shortcut for "binding to itself" (in the sense of RelativeSource.Self). Rather, {Binding} is equivalent to {Binding Path=.}, which binds to the current source.
 
Share this answer
 
Comments
Rakon22 19-Feb-17 11:26am    
OK Thanks but I still do not know how code should look like

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900