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