Home > Enterprise >  UserControl. Cannot find governing FrameworkElement or FrameworkContentElement for target element
UserControl. Cannot find governing FrameworkElement or FrameworkContentElement for target element

Time:03-04

I have DataGrid in my custom UserControl. And I want to bind Visibility Property (for DataGridTextColumn) to some Property on the code behind.

ReportDataGrid.xaml

<UserControl x:Class="DB_ME.Controls.ReportDataGrid"
         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:controls="clr-namespace:DB_ME.Controls"
         xmlns:local="clr-namespace:DB_ME.Controls"
         mc:Ignorable="d" 
         x:Name="Root"
         d:DesignHeight="450" d:DesignWidth="800">

<StackPanel>
    ...
    <DataGrid x:Name="ReportDG"
              ItemsSource="{Binding ElementName=Root, Path=SourceData, Mode=TwoWay}"
              AutoGenerateColumns="False">
                 <DataGrid.Columns>
                      <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=OneWay}" 
                                Visibility="{Binding ElementName=Root, Path=TempVisibility}"/>
                      ...
                 </DataGrid.Columns>
    </DataGrid>
    ...
</StackPanel>
</UserControl>

code behind ReportDataGrid.xaml.cs

public partial class ReportDataGrid : UserControl
{
    ...
    public Visibility TempVisibility
    {
        get
        {
            return Visibility.Collapsed;
        }
    }
    ...
}

But these code doesn't work. I got the next error:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=TempVisibility; DataItem=null; target element is 'DataGridTextColumn' (HashCode=51251115); target property is 'Visibility' (type 'Visibility')

Could someone help me? I alredy tried to use ProxyElement, but these doesn't help

CodePudding user response:

A DataGridColumn doesn't inherit any DataContext by default. That's why your binding fails.

You can get the binding to work by using a Freezable as suggested in this blog post:

public class BindingProxy : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}

XAML:

<DataGrid.Resources>
    <local:BindingProxy x:Key="proxy" Data="{Binding ElementName=Root}" />
</DataGrid.Resources>
...
<DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=OneWay}" 
                    Visibility="{Binding Data.TempVisibility, Source={StaticResource proxy}}"/>
 
  • Related