Upgrading WPF from .NET 4 to 4.5.2, DataGridTextColumn Visibility DataContext link broken

I upgraded the WPF project to .NET 4.5.2. In the xaml file, I have the following line.

<UserControl x:Class="Casa.Project.Client.Views.Projects.ProjectSearch" 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:Casa.Project.Core.Wpf.Controls;assembly=Casa.Project.Core.Wpf" mc:Ignorable="d" d:DesignWidth="700" x:Name="ProjectSearchWindow" > <UserControl.Resources> <DataGridTextColumn x:Key="PlanNumberColumn" Header="Project #" Visibility="{Binding DataContext.ShowPlanNumber, Source={x:Reference ProjectSearchWindow}}" Binding="{Binding ProjectNumber}" /> 

...

ReSharper underlines the entire “Visibility” tag by saying “Object reference is not set to an object instance”, which results in an error. When I load an old project that targets .NET 4, this error does not exist.

When I actually run the project, the entire table using the DataGridTextColumn does not show any values ​​(which load properly).

Are there any changes that have occurred from .NET 4 to .NET 4.5.2, which leads to this behavior? How to fix it?

0
visibility data-binding wpf
Jan 15 '16 at 18:25
source share
1 answer

So, I did not understand why the update broke it, but I figured out how to do it.

The Freezable class has the ability to pass a DataContext, although it is not part of the visual / logical tree. We use this for this fix.

Steps

First create a class that inherits from Freezable

 public class BindingProxy : Freezable { #region Overrides of Freezable protected override Freezable CreateInstanceCore() { return new BindingProxy(); } #endregion public object Data { get { return (object)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc... public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); } 

Second, release <helpers:BindingProxy x:Key="proxy" Data="{Binding}" /> right around your DataGridTextColumn. Change locally to the namespace of your BindingProxy class. Modify the DataGrid to match your xaml tag. Import the namespace for your BindingProxy into your xaml file, if necessary (for example, xmlns:helpers="clr-namespace:Casa.Project.Client.Helpers" )

 <DataGrid.Resources> <helpers:BindingProxy x:Key="proxy" Data="{Binding}" /> <DataGridTextColumn x:Key="PlanNumberColumn" Header="Project #" Visibility="{Binding Data.ShowPlanNumber, Source={StaticResource proxy}}" Binding="{Binding ProjectNumber}" /> </DataGrid.Resources> 

The status of the final code is approximately

 <UserControl x:Class="Casa.Project.Client.Views.Projects.ProjectSearch" 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:helpers="clr-namespace:Casa.Project.Client.Helpers" xmlns:controls="clr-namespace:Casa.Project.Core.Wpf.Controls;assembly=Casa.Project.Core.Wpf" mc:Ignorable="d" d:DesignWidth="700" x:Name="ProjectSearchWindow" > <UserControl.Resources> <helpers:BindingProxy x:Key="proxy" Data="{Binding}" /> <DataGridTextColumn x:Key="PlanNumberColumn" Header="Project #" Visibility="{Binding DataContext.ShowPlanNumber, Source={x:Reference ProjectSearchWindow}}" Binding="{Binding ProjectNumber}" /> 

Source: http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

0
Jan 15 '16 at 22:45
source share



All Articles