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); } }
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/
CamHart Jan 15 '16 at 22:45 2016-01-15 22:45
source share