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

  • SEARCH
  • Home
  • 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 3669712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:16:35+00:00 2026-05-19T02:16:35+00:00

I have a GridView and want to serialize column widths across sessions. My idea

  • 0

I have a GridView and want to serialize column widths across sessions. My idea of how to accomplish this is to attach a behavior to the GridViewColumns in such a way that each time the width of a column is changed the attached event handler is called and stores the new width. This already works well.

The only remaining problem:

How do I know in the event handler which GridViewColumn sent the event? I obviously need to know that in order to be able to store the width and later set the width on the correct column when restoring. Ideally I would like to use the name specified in XAML as column identifier.

Here is my code. XAML:

<GridView>
  <GridViewColumn x:Name="GridColumn0"
    HeaderTemplate="{StaticResource GridViewHeaderTemplate}" HeaderContainerStyle="{StaticResource GridViewHeaderStyle}" 
    Header="{x:Static strings:Strings.MainWindow_AppLog_Header_Severity}"
    behaviors:GridViewBehaviors.PersistColumnWidth="True">

C# (please scroll down – question at bottom):

// Register the property used in XAML
public static readonly DependencyProperty PersistColumnWidthProperty =
     DependencyProperty.RegisterAttached("PersistColumnWidth", typeof(bool), typeof(GridViewBehaviors),
     new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnPersistColumnWidthChanged)));

// Provide read access to the value
public static bool GetPersistColumnWidth(DependencyObject d)
{
    return (bool)d.GetValue(PersistColumnWidthProperty);
}

// Provide write access to the value (set from XAML)
public static void SetPersistColumnWidth(DependencyObject d, bool value)
{
    d.SetValue(PersistColumnWidthProperty, value);
}

// This gets called once when the XAML is compiled to BAML
// Set the event handler
private static void OnPersistColumnWidthChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    GridViewColumn column = sender as GridViewColumn;
    if (column == null)
        return;

    // Couple the UI event with a delegate
    if ((bool)args.NewValue)
        ((INotifyPropertyChanged)column).PropertyChanged += new PropertyChangedEventHandler(PersistWidth);
    else
        ((INotifyPropertyChanged)column).PropertyChanged -= new PropertyChangedEventHandler(PersistWidth);
}

// Deal with the events
static void PersistWidth(object sender, PropertyChangedEventArgs e)
{
    GridViewColumn column = sender as GridViewColumn;
    if (column == null)
        return;

    // We are only interested in changes of the "ActualWidth" property
    if (e.PropertyName != "ActualWidth")
        return;

    // Ignore NaNs
    if (column.ActualWidth == double.NaN)
        return;

    // Persist the width here
    // PROBLEM:
    // How to get a unique identifier for column, ideally its name set in XAML?
}
  • 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-19T02:16:35+00:00Added an answer on May 19, 2026 at 2:16 am

    Thanks for your answers, Robert, Thomas and Rune. I like Runes answer, but I found something even easier for my situation here:

    I changed the type of the attached property from bool to string and simply store the name of the column there. The relevant changes are below.

    XAML:

    <GridViewColumn 
      behaviors:GridViewBehaviors.PersistColumnWidth="MainWindow_AppLog_Column0">
    

    C#:

    public static readonly DependencyProperty PersistColumnWidthProperty = 
      DependencyProperty.RegisterAttached("PersistColumnWidth", typeof(string),
      typeof(GridViewBehaviors), new FrameworkPropertyMetadata(string.Empty, 
      new PropertyChangedCallback(OnPersistColumnWidthChanged)));
    
    static void PersistWidth(object sender, PropertyChangedEventArgs e)
    {
      // This now yields "MainWindow_AppLog_Column0"
      string columnID = GetPersistColumnWidth(column);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET GridView with a column mapped to a boolean. I want
I have used checkbox column in gridview. I want to check status of that
I have a gridview and I want to merge the headers of column (n)
I have a gridview, with some columns. I want to hide one column, but
I have a Gridview like this picture. Easyly in my last column, i keep
i want to display some information in a listview using the GridView. i have
I have a GridView where one column is bound to an object property containing
I have a GridView defined like this: <asp:GridView ID=myGridView ruant=server> <asp:BoundField DataField=myField /> <asp:CommandField
I have a GridView that allows editing the values in every column, in every
I have a web application (with C#). I have a GridView and want 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.