Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Working with WPF

Working with WPF

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpwpfcssdatabaselinq
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 8225180
    wrote on last edited by
    #1

    I am currently learning WPF and require some assistance in understanding some main concepts... My XAML contains numerous GUI elements, one being a DataGrid called 'dataGrid1' The DataGrid is populated in the main window using the following code fragment: MyDataGrid gridData = new MyDataGrid(); gridData.PopulateData(); dataGrid1.DataContext = gridData.dt; MyDataGrid is a class that is composed of a DataTable exposed through dt. PopulateData() performs a LINQ query on an EF context. The code above works fine and the grid shows the query results. What if I wanted to create a class which derives from DataGrid. How would such a class be accessible/linked/represented in the XAML?

    A reader from Montreal, Canada

    E 1 Reply Last reply
    0
    • M Member 8225180

      I am currently learning WPF and require some assistance in understanding some main concepts... My XAML contains numerous GUI elements, one being a DataGrid called 'dataGrid1' The DataGrid is populated in the main window using the following code fragment: MyDataGrid gridData = new MyDataGrid(); gridData.PopulateData(); dataGrid1.DataContext = gridData.dt; MyDataGrid is a class that is composed of a DataTable exposed through dt. PopulateData() performs a LINQ query on an EF context. The code above works fine and the grid shows the query results. What if I wanted to create a class which derives from DataGrid. How would such a class be accessible/linked/represented in the XAML?

      A reader from Montreal, Canada

      E Offline
      E Offline
      Ed Hill _5_
      wrote on last edited by
      #2

      WPF allows a lot of customisation to be done without needing to use inheritance, what is it you are wanting to achieve by inheriting from the DataGrid? If you decide you just want to in herit from the DataGrid anyway, a good point to start your research would be some research on DependencyProperties. If you want to add behaviour with out inheriting a good starting point would be understanding AttachedProperties.

      M 1 Reply Last reply
      0
      • E Ed Hill _5_

        WPF allows a lot of customisation to be done without needing to use inheritance, what is it you are wanting to achieve by inheriting from the DataGrid? If you decide you just want to in herit from the DataGrid anyway, a good point to start your research would be some research on DependencyProperties. If you want to add behaviour with out inheriting a good starting point would be understanding AttachedProperties.

        M Offline
        M Offline
        Member 8225180
        wrote on last edited by
        #3

        Thank-you for the reply. For the purpose of having the DataGrid events exist within the MyDataGrid object rather than my main window. Would this be a bad approach?

        E 1 Reply Last reply
        0
        • M Member 8225180

          Thank-you for the reply. For the purpose of having the DataGrid events exist within the MyDataGrid object rather than my main window. Would this be a bad approach?

          E Offline
          E Offline
          Ed Hill _5_
          wrote on last edited by
          #4

          If it is just one dataGrid used one time then generally speaking inheritance is not the best way to approach this. Although it is not the only way by any means to work with WPF, the MVVM design is very popular. If you have a set of requirements of what you are looking for your grid to do I will take a look and see if i can provide you with some sample code to get you started. One of the general principles of MVVM is seperating any code that involves your Data from the User Interface, using MVVM i find that it is quite rare that i am using EventHandlers for UI components. If you are just looking to seperate out your event handlers you could do something like this:

          public class MyWindow:Window
          {
          private MyDataCollection _gridData;

          public MyWindow()
          {
              InitializeComponent();
              \_gridData = new MyDataCollection();
              DataGrid1.RowAdded += \_gridData.DataRowAdded;
          }
          

          }

          public class MyDataCollection
          {
          public void DataRowAdded(object sender, EventArgs e)
          {
          }
          }

          I'm not saying the above code would be the right solution but you would not be inheriting a control you proabably don't need to if you are just aiming to seperate your Event Handling Code from your Window Code.

          M 1 Reply Last reply
          0
          • E Ed Hill _5_

            If it is just one dataGrid used one time then generally speaking inheritance is not the best way to approach this. Although it is not the only way by any means to work with WPF, the MVVM design is very popular. If you have a set of requirements of what you are looking for your grid to do I will take a look and see if i can provide you with some sample code to get you started. One of the general principles of MVVM is seperating any code that involves your Data from the User Interface, using MVVM i find that it is quite rare that i am using EventHandlers for UI components. If you are just looking to seperate out your event handlers you could do something like this:

            public class MyWindow:Window
            {
            private MyDataCollection _gridData;

            public MyWindow()
            {
                InitializeComponent();
                \_gridData = new MyDataCollection();
                DataGrid1.RowAdded += \_gridData.DataRowAdded;
            }
            

            }

            public class MyDataCollection
            {
            public void DataRowAdded(object sender, EventArgs e)
            {
            }
            }

            I'm not saying the above code would be the right solution but you would not be inheriting a control you proabably don't need to if you are just aiming to seperate your Event Handling Code from your Window Code.

            M Offline
            M Offline
            Member 8225180
            wrote on last edited by
            #5

            Sorry for the late reply. I was away from home the last 3 days. Yes, I was thinking on the line of something like this. For the moment, until I study recommended practices and patterns, this is not important. There is one item that is bothering me though. Why is it that when a given grid row is selected via code, the background is not highlighted in the same manner as when selecting the row via a mouse click. For instance: dataGrid1.SelectedIndex = 0; The background row is highlighted in a light-grey color. But clicking the row, the row is highlighted in blue. I've determined this is a focus issue. If the grid is forced to have the focus, the row is highlighted blue. If it loses focus, it changes to grey. In my particular case, it would be great to maintain the blue background on the row even if focus is lost. What would be the best way to do this?

            E 1 Reply Last reply
            0
            • M Member 8225180

              Sorry for the late reply. I was away from home the last 3 days. Yes, I was thinking on the line of something like this. For the moment, until I study recommended practices and patterns, this is not important. There is one item that is bothering me though. Why is it that when a given grid row is selected via code, the background is not highlighted in the same manner as when selecting the row via a mouse click. For instance: dataGrid1.SelectedIndex = 0; The background row is highlighted in a light-grey color. But clicking the row, the row is highlighted in blue. I've determined this is a focus issue. If the grid is forced to have the focus, the row is highlighted blue. If it loses focus, it changes to grey. In my particular case, it would be great to maintain the blue background on the row even if focus is lost. What would be the best way to do this?

              E Offline
              E Offline
              Ed Hill _5_
              wrote on last edited by
              #6

              try this. Note this should be added inside the following xaml node:

                      <Style.Triggers>
                          <!-- INSERT FROM LINK HERE -->
                      <Style.Triggers>
              
              M 2 Replies Last reply
              0
              • E Ed Hill _5_

                try this. Note this should be added inside the following xaml node:

                        <Style.Triggers>
                            <!-- INSERT FROM LINK HERE -->
                        <Style.Triggers>
                
                M Offline
                M Offline
                Member 8225180
                wrote on last edited by
                #7

                I placed a trigger on property IsSelected. Works fine. Thank-you.

                1 Reply Last reply
                0
                • E Ed Hill _5_

                  try this. Note this should be added inside the following xaml node:

                          <Style.Triggers>
                              <!-- INSERT FROM LINK HERE -->
                          <Style.Triggers>
                  
                  M Offline
                  M Offline
                  Member 8225180
                  wrote on last edited by
                  #8

                  Ed, Would you be able to provide some guidance to this problem: I have a DataGrid which is bound to a DataTable. Relevant XAML & code ('<' intentionally left off at start of each line): DataGrid Name="dataGrid1" IsReadOnly="True" ItemsSource="{Binding}" DataGrid.Columns> DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" /> DataGridTextColumn Header="Path" Binding="{Binding Path=Path}" /> /DataGrid.Columns> dataGrid1.DataContext = gridData.dt; // this is a DataTable with 3 columns. The 3rd dt column is not shown on the grid. The data table is sorted on the first column and contents are displayed. The user can sort on either of Name or Path columns/headings by clicking on them. My question, what is the best approach to update the grid data upon a user sorting on one of the columns? (This requires sorting the DataTable once again on the proper column) This is what I have observed: 1. Adding a Click event handler on DataGridColumnHeader does fire the event. I have observed that the value of columnHeader.SortDirection (in sender) is the CURRENT value, not the target value. Is it correct to say that a given column sort order goes through these phases: null --> ascending descending --> ascending ascending to descending I.E. By knowing the current state, the next state is determinable. 2. I can place a trigger in the DataGridColumnHeader's SortDirection property looking for Ascendiing/Descending/null but then what? Can I execute code against this? If so, can you show me a code fragment. Your thought on the above 2 approaches. Furthermore, what is the proper way of solving this issue in your opinion? Thank-you.

                  E 2 Replies Last reply
                  0
                  • M Member 8225180

                    Ed, Would you be able to provide some guidance to this problem: I have a DataGrid which is bound to a DataTable. Relevant XAML & code ('<' intentionally left off at start of each line): DataGrid Name="dataGrid1" IsReadOnly="True" ItemsSource="{Binding}" DataGrid.Columns> DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" /> DataGridTextColumn Header="Path" Binding="{Binding Path=Path}" /> /DataGrid.Columns> dataGrid1.DataContext = gridData.dt; // this is a DataTable with 3 columns. The 3rd dt column is not shown on the grid. The data table is sorted on the first column and contents are displayed. The user can sort on either of Name or Path columns/headings by clicking on them. My question, what is the best approach to update the grid data upon a user sorting on one of the columns? (This requires sorting the DataTable once again on the proper column) This is what I have observed: 1. Adding a Click event handler on DataGridColumnHeader does fire the event. I have observed that the value of columnHeader.SortDirection (in sender) is the CURRENT value, not the target value. Is it correct to say that a given column sort order goes through these phases: null --> ascending descending --> ascending ascending to descending I.E. By knowing the current state, the next state is determinable. 2. I can place a trigger in the DataGridColumnHeader's SortDirection property looking for Ascendiing/Descending/null but then what? Can I execute code against this? If so, can you show me a code fragment. Your thought on the above 2 approaches. Furthermore, what is the proper way of solving this issue in your opinion? Thank-you.

                    E Offline
                    E Offline
                    Ed Hill _5_
                    wrote on last edited by
                    #9

                    I'm afraid i have no experience with sorting on Grids, for most grids in the applications i work on we use a paid for grid component that handles the sorting for us. I'll have a quick look to see if i can figure out what you are after but i'd recommend you create a new post in the WPF forum with this question.

                    1 Reply Last reply
                    0
                    • M Member 8225180

                      Ed, Would you be able to provide some guidance to this problem: I have a DataGrid which is bound to a DataTable. Relevant XAML & code ('<' intentionally left off at start of each line): DataGrid Name="dataGrid1" IsReadOnly="True" ItemsSource="{Binding}" DataGrid.Columns> DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" /> DataGridTextColumn Header="Path" Binding="{Binding Path=Path}" /> /DataGrid.Columns> dataGrid1.DataContext = gridData.dt; // this is a DataTable with 3 columns. The 3rd dt column is not shown on the grid. The data table is sorted on the first column and contents are displayed. The user can sort on either of Name or Path columns/headings by clicking on them. My question, what is the best approach to update the grid data upon a user sorting on one of the columns? (This requires sorting the DataTable once again on the proper column) This is what I have observed: 1. Adding a Click event handler on DataGridColumnHeader does fire the event. I have observed that the value of columnHeader.SortDirection (in sender) is the CURRENT value, not the target value. Is it correct to say that a given column sort order goes through these phases: null --> ascending descending --> ascending ascending to descending I.E. By knowing the current state, the next state is determinable. 2. I can place a trigger in the DataGridColumnHeader's SortDirection property looking for Ascendiing/Descending/null but then what? Can I execute code against this? If so, can you show me a code fragment. Your thought on the above 2 approaches. Furthermore, what is the proper way of solving this issue in your opinion? Thank-you.

                      E Offline
                      E Offline
                      Ed Hill _5_
                      wrote on last edited by
                      #10

                      Ok had a little look at this, have you tried the following

                      For me adding the CanUserSortColumns="True" to the grid properties was enough to get sorting working, unless you need something more than visual sorting this should work for you.

                      M 1 Reply Last reply
                      0
                      • E Ed Hill _5_

                        Ok had a little look at this, have you tried the following

                        For me adding the CanUserSortColumns="True" to the grid properties was enough to get sorting working, unless you need something more than visual sorting this should work for you.

                        M Offline
                        M Offline
                        Member 8225180
                        wrote on last edited by
                        #11

                        Ed, This sorts the columns visually but the DataTable data bound to the grid is not sorted. Thank-you for your reply, I'll open a new subject.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups