用户控件。找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement
UserControl. Cannot find governing FrameworkElement or FrameworkContentElement for target element
我的自定义用户控件中有 DataGrid。我想将可见性 属性(对于 DataGridTextColumn)绑定到后面代码中的某些 属性。
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>
ReportDataGrid.xaml.cs
后面的代码
public partial class ReportDataGrid : UserControl
{
...
public Visibility TempVisibility
{
get
{
return Visibility.Collapsed;
}
}
...
}
但是这些代码不起作用。我收到下一个错误:
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')
有人可以帮助我吗?我已经尝试使用 ProxyElement,但这些都没有帮助
A DataGridColumn
默认不继承任何 DataContext
。这就是您的绑定失败的原因。
您可以按照 blog post:
中的建议,使用 Freezable
使绑定生效
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}}"/>
我的自定义用户控件中有 DataGrid。我想将可见性 属性(对于 DataGridTextColumn)绑定到后面代码中的某些 属性。
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>
ReportDataGrid.xaml.cs
后面的代码public partial class ReportDataGrid : UserControl
{
...
public Visibility TempVisibility
{
get
{
return Visibility.Collapsed;
}
}
...
}
但是这些代码不起作用。我收到下一个错误:
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')
有人可以帮助我吗?我已经尝试使用 ProxyElement,但这些都没有帮助
A DataGridColumn
默认不继承任何 DataContext
。这就是您的绑定失败的原因。
您可以按照 blog post:
中的建议,使用Freezable
使绑定生效
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}}"/>