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.

有帮助吗?

解决方案

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 });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top