Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:28:58+00:00 2026-05-26T03:28:58+00:00

.Net 3.5 I know that the columns doesn’t inherit the datacontext and by reading

  • 0

.Net 3.5

I know that the columns doesn’t inherit the datacontext and by reading other posts i thought this would work:

Visibility="{Binding RelativeSource={x:Static RelativeSource.Self},
                     Path=(FrameworkElement.DataContext).IsColumnNameVisible,
                     Converter={StaticResource boolToVisConverter}}"

However of course it doesn’t..
The output window does not complain, it seems that the resource i found but the viewmodel property is newer called.

This is the entire DG :

<tk:DataGrid                                        
            VirtualizingStackPanel.IsVirtualizing="False"                                        
            Grid.Column="0"
            AlternationCount="2"
            AreRowDetailsFrozen="True"
            AutoGenerateColumns="False"
            Background="Transparent"
            BorderThickness="0"
            CanUserAddRows="False"
            CanUserReorderColumns="True"
            CanUserResizeRows="False"
            GridLinesVisibility="None"
            ItemsSource="{Binding Employees}"
            SelectionMode="Single"
            ColumnHeaderStyle="{StaticResource columnHeaderStyle}"
            RowHeaderStyle="{StaticResource rowHeaderStyle}"
            CellStyle="{StaticResource cellStyle}"
            RowStyle="{StaticResource rowStyle}" 
            ContextMenu="{StaticResource columnHeaderContextMenu}">
    <tk:DataGrid.Resources>
        <ContextMenu x:Key="columnHeaderContextMenu" ItemsSource="{Binding ColumnHeaderContextMenuItems}" />
        <Style TargetType="{x:Type ScrollBar}">
            <Setter Property="Background" Value="Transparent"/>
        </Style>                                    
        <Style TargetType="{x:Type tk:DataGridColumnHeader}">
            <Setter Property="Background" Value="Transparent"/>
        </Style>
    </tk:DataGrid.Resources>
    <tk:DataGrid.Triggers>
        <EventTrigger RoutedEvent="tk:DataGridRow.MouseDoubleClick">
            <EventTrigger.Actions>
                <BeginStoryboard Storyboard="{StaticResource showDetailGrid}"/>
            </EventTrigger.Actions>
        </EventTrigger>
    </tk:DataGrid.Triggers>
    <tk:DataGrid.Columns>
        <tk:DataGridTextColumn IsReadOnly="True" Header="test" Binding="{Binding Name, Mode=OneWay}" Visibility="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(FrameworkElement.DataContext).IsColumnNameVisible, Converter={StaticResource boolToVisConverter}}"  />
    </tk:DataGrid.Columns>
</tk:DataGrid>

I have read pretty much every single solution to this problem and nothing works..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:28:58+00:00Added an answer on May 26, 2026 at 3:28 am

    DataGridColumns are not part of visual tree so they are not connected to the data context of the DataGrid.

    For them to connect together use proxy element approach like this…

    1. Add a proxy FrameworkElement in your ancestor panel’s Resources.

    2. Host it into an invisible ContentControl bound to its Content.

    3. Use this ProxyElement as StaticResource for data context source in your visibility binding.

       <StackPanel>
           <StackPanel.Resources>
              <local:BooleanToVisibilityConverter
                     x:Key="BooleanToVisibilityConverter" />
      
              <FrameworkElement x:Key="ProxyElement"
                                DataContext="{Binding}"/>
           </StackPanel.Resources>
           <ContentControl Visibility="Collapsed"
                       Content="{StaticResource ProxyElement}"/>
           <DataGrid AutoGenerateColumns="False">
               <DataGrid.Columns>
                   <DataGridTextColumn
                          Visibility="{Binding DataContext.IsTextColumnVisibile,
                                               Source={StaticResource ProxyElement},
                                               Converter={StaticResource
                                                   BooleanToVisibilityConverter}}"
                          Binding="{Binding Text}"/>
               </DataGrid.Columns>
           </DataGrid>
       </StackPanel> 
      

    Apart from DataGridColumn, the above approach also works great to connect DataContext to Popups and ContextMenus (i.e. any element that is not connected to the visual tree).

    Silverlight Users

    Sadly setting contents of content controls with any framework elements is not allowed in silverlight. So the workaround would be (this is just a guidance code for silverlight) …

    1. Change the framework element resource to something lightweight like a Textblock. (Silverlight does not allow specifying static resource of FrameworkElement type.)

       <StackPanel.Resources>
           <TextBlock x:Key="MyTextBlock" />
      
    2. Write an attached property to hold text block against the content control.

       <ContentControl Visibility="Collapsed" 
                       local:MyAttachedBehavior.ProxyElement="{StaticResource MyTextBlock}" />
      
    3. In the attached dependency property changed event handler, set the bind the data context of the content control to the text block’s.

        private static void OnProxyElementPropertyChanged(
            DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
              if (depObj is ContentControl && e.NewValue is TextBlock)
              {
                  var binding = new Binding("DataContext");
                  binding.Source = depObj;
                  binding.Mode = OneWay;
                  BindingOperations.SetBinding(
                      (TextBlock)e.NewValue, TextBlock.DataContextProperty, binding);
              }
        }
      

    So this way the textblock may not be connected to the visual tree but will probably be aware of the data context changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know that in vb.net you can just do Exit Sub but i would
I know that .NET is JIT compiled to the architecture you are running on
I know that .NET assembly is self-descriptive because of the metadata. Now suppose I
So, I've spent enough time using ASP.NET webforms to know that I'd almost rather
I know that the .NET framework looks for referenced DLLs in several locations Global
I know that ASP.NET MVC will allow me to swap in various View engines
I know that ASP.NET MVC 1.0 is bin-deployable as explained in Phil Haack's article.
I know that the asp.net repeater doesnt have a Client side object model, and
I know that the static objects in .Net managed world are loaded in Loader
I know that in the next version of ASP.NET we'll finally be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.