将WPF从.NET 4更新为4.5.2,DataGridTextColumn可见性DataContext引用被破坏 [英] Update WPF from .NET 4 to 4.5.2, DataGridTextColumn Visibility DataContext reference broken
问题描述
< 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 =PlanNumberColumnHeader =Project#Visibility ={Binding DataContext.ShowPlanNumber,Source = {x:Reference ProjectSearchWindow}}Binding ={Binding ProjectNumber}/>
...
ReSharper强调整个可见性标签,表示对象引用未设置为对象的实例,这会产生错误。当我加载目标为.NET 4的旧项目时,该错误不存在。
当我实际运行项目时,使用DataGridTextColumn的整个表不会显示任何值(正在正确加载)。
是否有从.NET 4到.NET 4.5.2发生的一些更改,导致此行为?如何修复?
所以我没有弄清楚为什么升级破解了,但是我确实发现可以让它工作的方式。
Freezable类有能力将DataContext传递给它,即使它不是视觉/逻辑树的一部分。
步骤
首先,创建继承自Freezable的类
public class BindingProxy:Freezable
{
#region覆盖Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
公共对象数据
{
get {return(object)GetValue(DataProperty); }
set {SetValue(DataProperty,value); }
}
//使用DependencyProperty作为Data的后备存储。这使得动画,样式,绑定等...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register(Data,typeof(object),typeof(BindingProxy),new UIPropertyMetadata(null)) ;
}
其次,删除< helpers:BindingProxy x: Key =proxyData ={Binding}/>
在您的DataGridTextColumn的周围。将本地更改为BindingProxy类的命名空间。更改DataGrid以匹配您的根xaml的标签。如果需要,将BindingProxy的命名空间导入到xaml文件中(如 xmlns:helpers =clr-namespace:Casa.Project.Client.Helpers
)
< DataGrid.Resources>
< helpers:BindingProxy x:Key =proxyData ={Binding}/>
< DataGridTextColumn x:Key =PlanNumberColumnHeader =Project#Visibility ={Binding Data.ShowPlanNumber,Source = {StaticResource proxy}}Binding ={Binding ProjectNumber}/>
< /DataGrid.Resources>
最终代码状态大致
< 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 =proxyData ={Binding}/>
< DataGridTextColumn x:Key =PlanNumberColumnHeader =Project#Visibility ={Binding DataContext.ShowPlanNumber,Source = {x:Reference ProjectSearchWindow}}Binding ={Binding ProjectNumber}/>
资料来源: http://www.thomaslevesque.com/2011/03/21/wpf -with-to-bind-to-data-when-the-datacontext - 不是继承/
I've upgraded a WPF project to .NET 4.5.2. In a 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, saying "Object reference not set to an instance of an object", which produces an error. When I load up the old project that targets .NET 4, that error doesn't exist.
When I actually run the project the entire table that uses DataGridTextColumn doesn't show any of the values (which are getting loaded properly).
Is there some change that occurred from .NET 4 to .NET 4.5.2 that results in this behavior? How do I fix it?
So I didn't figure out why the upgrade broke it, but I did figure out a way to make it work.
The Freezable class has the ability to get DataContext passed to it even though it's not part of the visual/logical tree. We take advantage of that 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, drop <helpers:BindingProxy x:Key="proxy" Data="{Binding}" />
right around your DataGridTextColumn's. Change local to the namespace of your BindingProxy class. Change DataGrid to match you're root xaml's tag. Import the namespace for your BindingProxy into your xaml file if necessary (like 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>
Final code state roughly
<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}" />
这篇关于将WPF从.NET 4更新为4.5.2,DataGridTextColumn可见性DataContext引用被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!