Working with WPF
-
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
-
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
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.
-
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.
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?
-
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?
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.
-
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.
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?
-
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?
try this. Note this should be added inside the following xaml node:
<Style.Triggers> <!-- INSERT FROM LINK HERE --> <Style.Triggers>
-
try this. Note this should be added inside the following xaml node:
<Style.Triggers> <!-- INSERT FROM LINK HERE --> <Style.Triggers>
I placed a trigger on property IsSelected. Works fine. Thank-you.
-
try this. Note this should be added inside the following xaml node:
<Style.Triggers> <!-- INSERT FROM LINK HERE --> <Style.Triggers>
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.
-
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.
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.
-
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.
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.
-
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.
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.