I have a C# CLI program that scans for missing Windows updates and writes them to command line or serializes them to XML depending on the flag passed in. I'm trying to build a WPF component to this but am unsure of a few things. Specifically I'd like to write all missing updates to a grid in the center of my WPF main window. The appearance would be something like this (with gridlines between the fields):
NAME SEVERITY DETECTED
Security Update for Windows 7 (KB1234567) Important 3/9/2014
Security Update for Windows 7 (KB7654321) Critical 3/9/2014
My specific questions:
What type of control would I need to add to the window to house this data?
How do I send the data (detected missing update names and properties) to the grid for display?
How can I set the control so that it is collapsed (or invisible) when no missing updates are detected?
Will I need to add a scrollbar to the grid or will one display automatically?
Apologies for the simple questions. I'm really just looking for some examples to get started, and I haven't been able to find anything thus far that meets my needs.
What type of control would I need to add to the window to house this
data?
DataGrid control is what you are looking for.
How do I send the data (detected missing update names and properties)
to the grid for display?
Bind ItemsSourceof DataGrid to ObservableCollection<T> where T will be class containing data with properties Name, Severity and Detected.
How can I set the control so that it is collapsed (or invisible) when
no missing updates are detected?
Add a DataTrigger to check if ItemsSource collection contains no data, collapse the visibility.
Will I need to add a scrollbar to the grid or will one display
automatically?
DataGrid internally use ScrollViewer. No need to add explicitly.
Refer to the dataGrid samples here and here.
As an alternative DataGrid can offer ListView control, it will be little "easier" than the DataGrid, he also supports the ability to sort columns. For him also need to bind a ItemsSource collection to display:
The ListView control provides the infrastructure to display a set of data items in different layouts or views. For example, a user may want to display data items in a table and also to sort its columns.
Example in MSDN.
Little add some notes to the wonderful answer of #RohitVats, all that has been said about DataGrid also applies to ListView:
How can I set the control so that it is collapsed (or invisible) when no missing updates are detected?
In this situation, I advise you to adhere to the principle of MVVM. Use Binding and Commands to create an independent application. You want to create property (for example IsEnabled) in Model / ViewModel and use bindings to set them in the View, in order to avoid apply directly to the Control. WinForms style app or "regular" applications creates a strong connection between logic and UI, which subsequently impedes further change and application maintenance.
Related
In a Windows 10 UWP application I'd like to bind a collection of simple objects to a GridView or ListView and have the GridView or ListView autogenerate the columns based on the properties of the object rather than having to manually declare the columns and {Binding Path=SomePropertyName} on a TextBlock in the XAML.
This doesn't look possible.. is it?
Is there a different type of control other than GridView or ListView that will allow this behaviour?
Note: This is not WPF
TL;DR: This is not possible out of the box with the GridView or ListView controls.
In UWP a GridView is:
A control that displays data items in rows and columns.
The ListView is quite similar but only shows the items stacked in 1 dimension, by default vertical.
The DataGrid control (what this is typically called) is currently (as of SDK build 14393) not available in the default control set. With "some" effort you could write your own control for this behavior.
There are however multiple 3rd party solutions available, just google/bing for UWP DataGrid. Here are some of them:
MyToolkit.Extended NuGet package, more info on their GitHub page.
Libraries that might need a paid subscription/license:
SyncFusion
ComponentOne
Infragistics
You might find even more alternatives.
Can you write it in c# instead of xaml? Maybe it would be possible then, as long as you can access the container (gridview or whatever you use) beyond the constructor of your class. I'm not entirely sure if you can generate a new grid and switch on the fly but you could test it easily.
I want to display multiple columns (2) and multiple rows. Data consists of Image and label.
Can anyone suggest a way todo it or provide a sample code. I find it challenging as to know, how to bind the data with multiple rows.
I do not have any code to show as I am not able to come up with anything close to what I want to acheive.
You could consider the RepeaterView control (https://github.com/XLabs/Xamarin-Forms-Labs/wiki/RepeaterView) that allows you to bind a control template (e.g. consisting of an Image and Label) to a set of objects as bound or set to the ItemsSource property - it will then create an instance of the control template for every item in the collection, and any binding references within the control template will relate to the individual items in the collection. This is easy to include in your project in isolation.
Alternatively if your requirements are more involved I would recommend the free DevExpress grid component (https://components.xamarin.com/view/devexpress-grid).
I'm writing a BHO that uses an ElementHost to host a WPF User Control. Within the user control, I have a DataGrid that binds to an Observable Collection. Everything functions fine, except that the content is not being read by screen readers (I'm using NVDA to test, but QA is using JAWS).
I'm restricted from copy & pasting code on a public forum, but I can describe the layout that I'm creating. There are two datagrid. One contains all of the items from the Observable Collection and the other is a subset of the items. Each datagrid is in a separate Tab Item of a TabControl. As I stated, there is an ObservableCollection that holds my business objects. Each object binds to a row in the datagrid. Several of the columns require that I display multiple properties from the business object, so I'm using the DataGridTemplateColumn. Within the CellStyle, I have 3 DataTemplates set up; one for edit, one for add and one for view. The view DataTemplate is exactly the same as the DataGridTemplateColumn.CellTemplate.
One of the columns holds my action buttons. One of the buttons is an edit button which simply applies the edit template to the rows cells. Outside of the datagrid, I have a button that will add a new default item to the ObservableCollection, call UpdateLayout on the datagrid, then set the DataTemplate of the new item to the add template.
There is also a button that will grab information from a remote server, convert it to business objects and add them to the Observable Collection. The datagrid loads the new information with no problems.
When I use the function to pull the objects from the database, none of the information will be read by the screen reader. If I click the edit button, everything is read as expected. After returning to the "view" DataTemplate, everything reads as expected. If I use the Add button, everything reads as expected.
To make this more complicated, if I edit an item in one tab page and get it to be read, then go to the other tab page and come back, it's no longer reading.
I have a feeling that it's related to the how binding and templates interact, but I don't know enough about either to figure out a direction to go in resolving this.
Any help would be appreciated.
EDIT: While I was creating the dummy project to show the issue, I discovered that the problem is not just limited to Template Columns. I created a business object with string properties, created an ObservableCollection with 10 objects in it and bound each property to a DataGridTextColumn, and it only reads the grid name and column index...the contents are never read.
I am having a form with different type of controls like Text Box, Drop downs, Check box, Radio buttons etc. All these controls are loaded dynamically from database at run time.
I want to perform validation on Text box on conditional basis. For example, If we have selected any value in drop down, then you must have to fill details in Text box. Otherwise text box details are not required.
I am open to use database to perform this task and I am using MVVM pattern in my project.
Any help on this is highly appreciated.
Thanks.
(I started this as a comment, but it ended up being too long).
In theory you have access to all these controls and their values in your ViewModel.
Without knowing the specifics of your program, it's difficult to suggest anything useful, but in essence you need to expose some more properties from your ViewModel (probably boolean) which will be calculated based on the values in your controls. Then you need to bind IsEnabled properties on your controls to these new properties.
Sounds simple, but I think you have some architectural problems which will make it difficult to implement what I suggested above. In order for this to work and automatically update your controls whenever other controls' content change, your ViewModel needs to implement INotifyPropertyChanged and raise PropertyChanged event every time you update one of those boolean properties.
I think what you're trying to do could be achieved with ItemsControl and DataTemplates (and maybe DataTemplateSelectors). This will allow you to store "data" in your ViewModel (say List or something more specific) without referencing the actual Controls and the relevant DataTemplates will add the right controls for different data types you have in your ViewModel.
I have a DataGrid that is showing some data via a PagedCollectionView with one group definition. I have created a Style for the corresponding DataGridRowGroupHeader under which I have added a ControlTemplate containing an additional TextBlock and a spacing Rectangle. I would like to bind the widths of these controls to the widths of particular columns, but I am struggling to get this working. I would also like to bind the Text property of the TextBlock to a value.
I tried binding the widths via the Width property of a Rectangle in resources but this didn't work (possibly because the Rectangle was never drawn and therefore didn't calculate it's layout).
However, I believe both sets of bindings can be performed with some use of one or more ValueConverter implementations, but I was wondering if there was a better way. Can any of this be achieved through the definition of a ControlTemplate?
After some trial and error I was able to customize my row group headers. The key to unlocking the solution involved both the RowGroupHeaderStyles property and the LoadingRowGroup event on the DataGrid.
By defining one or more styles for the groups, I was able to customize the control template to include additional named elements. I then used the event to gain access to those elements and either set or bind the relevant values to show the information I required. The only stumbling I had related to binding the size of controls, which I eventually worked around by saving a reference to each row and setting those sizes when it was necessary to refresh them rather than relying on bindings. This may be specific to my project so your mileage may vary.
Update
JDM asked how you get the controls to perform binding etc. in the LoadingRowGroup event handler. You can get the row header from the DataGridRowGroupHeaderEventArgs.RowGroupHeader property of the event arguments and then use the VisualTreeHelper to get the child controls of the header. Once you have the controls, you can bind to them in code as you would any other control.