WPF DataGrid with selectable rows and button

Hello all,
I am new to C# but purchased Linqpad to try and learn some new skills, and have to an extent cutting and pasting and meddling from the internet.
I have managed to get my results from my Gmail API query into a list of class objects, but would like to have them in a WPF DataGrid with a button that I can use the selected rows from the results by CTRL left clicking to select, then do something with the selected items.

Could someone please give me a small example of this? I am going in circles and seem to have fallen into some ""double quote"" syntax hell with the XAML.

The resulting use will be to mark the selected Gmail emails as read.

Thanks for looking.


  • Can you post what you have so far? The whole file contents or a link to it hosted somewhere like github.com. Remove any confidential info.

  • I forgot you can use the Instant Share feature.

    LINQPad - Upload to Instant Share
    What is shared?
    LINQPad uploads your query text and properties (references, namespaces, app.config settings, and so on). If your query has a connection, then the connection properties are also shared, with the exception of the following:

    Anything entered into the Username and Password boxes
    Anything entered into the AccountKey box (for OData connections)
    I have a password or account key in my source code. How do I safely share the query?
    Instead of hard-coding your password, do the following:

    string password = Util.GetPassword ("My Azure Storage Account");
    This utilizes LINQPad's built-in password manager. Go to File | Password Manager for more info.

    I accidentally uploaded a query with sensitive data. Can I delete it?
    Yes. Upload the same query again (from the same machine). After it completes, click the 'Delete' button.

  • @stephensmitchell
    With regard to WPF DataGrid , I do not have anything, I have tried to create XAML file in Visual Studio then import or create a string of XAML for XamlReader but am missing something about escaping double quotes. Just a basic example from a List of classes to WPF DataGrid would suffice.

  • have tried to create XAML file in Visual Studio then import or create a string of XAML for XamlReader but am missing something about escaping double quotes

    I think if you import the .xaml file from a stream:

    using(var fs = new FileStream("your file.xaml"))
        Window page = (Window)System.Windows.Markup.XamlReader.Load(fs);

    This will properly escape quotes.

  • Ah ok thanks, I will try this, I had been trying to use System.Windows.Markup.XamlReader.Parse

  • I cant seem to get my stupid head around this , I have managed to get some output , but doesn't seem to respect the XAML string for coloring alternate rows . I have up loaded what I have working so far. I have no idea how to the get the msgIDs for only the selected rows I click on to work with later on.

    This XAML stuff is too complicated for me , can I do this directly in some Linqpad utility?

    anyway this is what I have so far , mostly cut and paste from the internet


  • This XAML stuff is too complicated for me

    Me too. I have only ever done the very basic, like changing the colour of the Alternating Row, which on the dataGrid I would use

        var list = new DataGrid();
        list.AlternationCount = 2 ;
        list.AlternatingRowBackground = new SolidColorBrush(Colors.Cyan);

    To get the selected items, you can iterate through list.SelectedItems

        button.Click += (o, e) =>
            foreach (AttachmentList item in list.SelectedItems)
                var msgid = item.msgID;
                // ..
  • Working with XAML in code is error-prone, and harder to update without Visual Studio XAML tools. The purpose of my filestream post was to suggest loading all the XAML from a file to then use in LINQPad. It's easier for
    me to use only code for WPF in LINQPad rather than working with XAML as a string otherwise I use Visual Studio.

    I attached a quick concept of how I build WPF tools without XAML. This is a simple example not sure what your end goal is but here is something.

    This is VB code, but it should be easily converted to C# (https://converter.telerik.com/). Most of the code is one-to-one with C#.


  • @sgmoore
    Wow thanks so much, thats working now, so simple when you know how, I have been banging my head with all this XAML syntax hell, I thought I had to use XAML to create my DataGrid and buttons.
    Thanks for your help your example is great, I haven't looked at VB for years. I did try what you said regarding file stream, I had a working Xaml file in Visual Studio but when importing it had a lot Xamlreader parser errors , even though it compiled and ran in VS.

    Thanks again to all

Sign In or Register to comment.