2

I'm trying to create my own GridViewColumn and having some issus with binding.

Can someone explain to me why the following Header-binding does work

<GridViewColumn x:Class="interneProzesse_UebersetzungstoolNS.TranslateGridViewColumn"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:conv="clr-namespace:interneProzesse_UebersetzungstoolNS.Converter"
         xmlns:hk="clr-namespace:interneProzesse_UebersetzungstoolNS.Hilfsklassen"
         xmlns:local="clr-namespace:interneProzesse_UebersetzungstoolNS"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"Header="{Binding RelativeSource={RelativeSource Self}, Path=Sprache, UpdateSourceTrigger=PropertyChanged}">
</GridViewColumn>

while it fails here?

<GridViewColumn x:Class="interneProzesse_UebersetzungstoolNS.TranslateGridViewColumn"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:conv="clr-namespace:interneProzesse_UebersetzungstoolNS.Converter"
         xmlns:hk="clr-namespace:interneProzesse_UebersetzungstoolNS.Hilfsklassen"
         xmlns:local="clr-namespace:interneProzesse_UebersetzungstoolNS"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<GridViewColumn.Header>
    <GridViewColumnHeader Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:TranslateGridViewColumn}}, Path=Sprache, UpdateSourceTrigger=PropertyChanged}"/>
</GridViewColumn.Header>
<GridViewColumn>

Sprache is a Property of my TranslateGridViewColumn which inherits from GridViewColumn.

Marc
  • 12,706
  • 7
  • 61
  • 97
Florian Gl
  • 5,984
  • 2
  • 17
  • 30
  • Do you get any `BindingExpression` error in your debug output window? – DHN Mar 14 '13 at 09:19
  • Why don't you use the first form if it is working? – nemesv Mar 14 '13 at 09:21
  • @DHN Yes, it sais "Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='interneProzesse_UebersetzungstoolNS.TranslateGridViewColumn', AncestorLevel='1''." But I'd like to know why it cant find, because the `TranslateGridViewColumn` is definitivly an ancestor. – Florian Gl Mar 14 '13 at 09:24
  • @nemesv because I need the second form for other bindings where I dont have an alternative. – Florian Gl Mar 14 '13 at 09:26
  • Hmm, no idea...perhaps [Snoop](http://snoopwpf.codeplex.com/) can help you. - Btw, you're not really implementing your application in German, are you? O.o – DHN Mar 14 '13 at 09:45
  • @DNH :D Yes I am, but its an application just for internal use, so it should be ok. Usually i write my code in english. ;) – Florian Gl Mar 14 '13 at 10:20
  • @DNH: It's a translation software, so it doesn't matter in which language you implement it. – Marc Mar 14 '13 at 10:30
  • @Marc yes, but it doesnt translate its code :D – Florian Gl Mar 14 '13 at 10:50
  • But it COULD, that's the point... Just joking... – Marc Mar 14 '13 at 11:22

1 Answers1

0

A few minutes after posting the question, I found my answer in this thread. The answer basically sais the GridViewColumn wont be added to the visual tree, so bindings which uses this visual tree (e.g. FindAncestor) cant work.

So I subsribed to the Loaded-events of the elements which I bind to (e.g. GridViewColumnHeader) and did the binding in Code-behind:

BindingOperations.SetBinding(sender as GridViewColumnHeader, GridViewColumnHeader.ContentProperty, new Binding("Sprache") { Source = this, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Mode = BindingMode.OneWay });
Community
  • 1
  • 1
Florian Gl
  • 5,984
  • 2
  • 17
  • 30