Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Modifying XAML in code-behind for GridViewColumn

c# xaml xmlreader gridview grid

  • Please log in to reply
4 replies to this topic

#1 crokett

crokett

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 109 posts
  • Programming Language:C#, Perl
  • Learning:C#, Perl, Transact-SQL

Posted 27 September 2012 - 04:27 PM

I also have this on Stackoverflow, maybe somebody here has an answer.


I have a Window that contains a ListView. I use the same Window for several different searches so after I run a search I add a GridView and columns to the Listview in code, then I set the datasource to the ObservableCollection created by the search:

GridViewColumn FCPortColumn = new GridViewColumn();

      FCPortColumn.Header = "FC Port";
      FCPortColumn.DisplayMemberBinding = new Binding("FCPort");
      FCPortColumn.Width = Double.NaN;
      FCPortColumn = (GridViewColumn)XamlReader.Load(xmlReader);

 GridView grid = new GridView();
 grid.Columns.Add(FCPortColumn);
  ResultsListView.Add(grid);

This code is supposed to replace the XAML on the column. I am adding a property called GridViewSort so I can sort the column. I found the GridViewSort here:
http://www.thomaslev...der-is-clicked/
My problem is I get an exception when I run the XmlReader.Create() because it doesn't know what GridViewSort is. The default namespace for the GridviewColumn is pointing back to MS. Is there a way to add to the namespace? I can use the GridViewSort property on a pre-defined column in the XAML.cs file for the Window, but I wanted to be able to add the column in code-behind and modify the XAML there.
Is there another way to do what I want to do? Maybe I pre-define all the columns I might need and then remove what I don't need, or hide them?


/get current XAML
string savedxaml = XamlWriter.Save(FCPortColumn);

//create new string
	 int index2 = savedxaml.IndexOf("\" ");
	 string insert = savedxaml.Insert(index2 + "\" ".Length,
		 String.Format(@" GridViewSort.PropertyName=""{0}"" ", "FCPort"));
	 //now replace the XAML on the column
	 StringReader stringReader = new StringReader(insert);
	 XmlReader xmlReader = XmlReader.Create(stringReader);

  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 September 2012 - 05:13 PM

BTW, why you are doing this --
FCPortColumn.Width = Double.NaN;
?

Is there any chance that you can attach your project and we can just download and debug?
  • 0

#3 crokett

crokett

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 109 posts
  • Programming Language:C#, Perl
  • Learning:C#, Perl, Transact-SQL

Posted 27 September 2012 - 06:12 PM

That is supposed to set the width to Auto. The project is rather large. I attached the .CS files for the GridViewSort and the .XAML and .cs files for the Window, although the query.xaml.cs is a partial class so that might not help. I hope that's enough. Queries.XAML.cs contains a private method called AddSortToXAML, it is supposed to modify the XAML code for the column. The exception thrown is

'Cannot set unknown member '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}GridViewSort.PropertyName'.' Line number '1' and line position '63'.

and it is thrown here:

private void AddSortToXAML(GridViewColumn column, string sort)
        {
            string savedxaml = XamlWriter.Save(column);
            int index2 = savedxaml.IndexOf("\" ");
            string insert = savedxaml.Insert(index2 + "\" ".Length,
                String.Format(@" GridViewSort.PropertyName=""{0}"" ", sort));
            StringReader stringReader = new StringReader(insert);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            column = (GridViewColumn)XamlReader.Load(xmlReader);  //exception is thrown on this line.
        }

The GridViewcolumn default schema doesn't know what the GridViewSort is. I tested the above method and I can set properties that GridViewColumn schema knows about already. The question is, how to change the default schema or add to it for the GridViewColumn? Adding it to the XAML for the Query window object didn't help.

Attached Files


  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 September 2012 - 09:55 PM

As of my understanding, to create a dependency property (as well as attached property), we have to create a public static object of type DependencyProperty using Register method (or RegisterAttached in case attached property) with have 'property' appended after the dependency/attached property name and a .NET property wrapper (setter & getter) which must be non static -- see there on MSDN. But in this case, you made the property wrapper with static. I'm not sure whether this is right or not but I never did this way. Following is your code that I'm saying about --
public static string GetPropertyName(DependencyObject obj) /* should not it be non static??? */
{
    return (string)obj.GetValue(PropertyNameProperty);
}

public static void SetPropertyName(DependencyObject obj, string value)  /* should not it be non static??? */
{
    obj.SetValue(PropertyNameProperty, value);
}

// Using a DependencyProperty as the backing store for PropertyName.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty PropertyNameProperty =
    DependencyProperty.RegisterAttached(
        "PropertyName",
        typeof(string),
        typeof(GridViewSort),
        new UIPropertyMetadata(null)
    );


As I said, I'm not sure -- is there any chance that the system is not able to find the attached property for this reason?

I saw that there are other dependency and attached properties there that has the same static declaration.
  • 0

#5 crokett

crokett

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 109 posts
  • Programming Language:C#, Perl
  • Learning:C#, Perl, Transact-SQL

Posted 28 September 2012 - 05:41 AM

I looked at that yesterday. I don't think that's the issue... the GridViewColumn doesn't know what the property is that I am trying to modify because it isn't part of the default XAML. that creates the column. I instantiate a GridViewColumn and then try to tweak the XAML. It fails because XMLReader.Load() is parsing the supplied XAML and doesn't know what the new property is, since it is not included in the default schema from Microsoft. So I need some way to modify the default schema to include the local namespace. I tested tweaking the XAML with a property that the GridViewColumn already knows about. That works fine. It also works if I add the GridViewColumn to Queries.XAML ahead of time, but that gives pre-defined columns and I am trying to re-use this Window and add columns as-needed in code-behind.

All I want is to be able to sort columns. If there's another way,I am happy to use that way to do it.
  • 0





Also tagged with one or more of these keywords: c# xaml, xmlreader, gridview, grid

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download