2
votes

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.

1
Do you get any BindingExpression error in your debug output window?DHN
Why don't you use the first form if it is working?nemesv
@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
@nemesv because I need the second form for other bindings where I dont have an alternative.Florian Gl
Hmm, no idea...perhaps Snoop can help you. - Btw, you're not really implementing your application in German, are you? O.oDHN

1 Answers

0
votes

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 });