Hallo zusammen,
Ich habe ein Control (ZlsUnitBrowserActive) mit zwei DependencyProperties MaxWidthIdentifier/PropertiesDataTemplate. Wenn ich versuche die beiden properties über "RelativeSource" anzubinden, bekomme ich immer folgende Fehlermeldung:
Fehlermeldung:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='PlcFramework.Zls.Production.ProductionControls.ZlsUnitBrowserActive', AncestorLevel='1''. BindingExpression:Path=PropertiesDataTemplate; DataItem=null; target element is 'DataGridTemplateColumn' (HashCode=30225241); target property is 'CellTemplate' (type 'DataTemplate')
<Style TargetType="productionControls:ZlsUnitBrowserActive">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="productionControls:ZlsUnitBrowserActive">
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate
MaxWidth="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=productionControls:ZlsUnitBrowserActive}, Path=MaxWidthIdentifier}"
CellTemplate="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=productionControls:ZlsUnitBrowserActive}, Path=PropertiesDataTemplate}"/>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
Nun habe ich bereits die Lösung hier gefunden: https://www.mycsharp.de/wbb2/thread.php?postid=3787237
<Style TargetType="productionControls:ZlsUnitBrowserActive">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="productionControls:ZlsUnitBrowserActive">
<DataGrid>
<DataGrid.Resources>
<xamlHelper:BindingProxy x:Key="DataContextBindingProxy" Data="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate
MaxWidth="{Binding Path=Data.MaxWidthIdentifier, Source={StaticResource DataContextBindingProxy}}"
CellTemplate="{Binding Path=Data.MaxWidthIdentifier, Source={StaticResource PropertiesDataTemplate}"/>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
Kann mir bitte einer eine Erklärung geben warum ich das ganze so lösen muss oder gibt es vielleicht noch eine bessere bzw. elegantere Lösung?
Danke!
Ich glaube, TemplateBinding wäre hier die einfachste Möglichkeit.
Weeks of programming can save you hours of planning
Habe eine Erläuterung gefunden: https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
The message is rather cryptic, but the meaning is actually quite simple: WPF doesn’t know which FrameworkElement to use to get the DataContext, because the column doesn’t belong to the visual or logical tree of the DataGrid.
Ich glaube,
> wäre hier die einfachste Möglichkeit.
Das habe ich bereits getestet gehabt.
Thread kann geschlossen werden