How can i modify XAML elements from another class (C#-WPF) - c#

I am trying to connect a class called Engine with XAML elements, to be more specific I have a LevelWindow.xaml where a grid exists. I want to split that grid (in rows and columns ) from Engine class, the split code exists, but when the programs is running Engine is not changing grid from LevelWindow .... I tryed to semi-use bindings, but i cant find any way to Bind the GRID .... some sugestions ?
Engine

If you absolutely want to use your existing splitting code and that it doesn't support binding and/or notify property changed, you can perhaps find some way to perform what you want using a ContentControl instead of your Grid in your xaml file.
Then Bind the contentControl content property to a grid that you instantiate in your Engine (thus you can access and modify it's inner properties like grid column/rows).
Yet it's quite horrible to perform graphic control instantiation in your business class...
Link to MSDN contentcontrol :
https://msdn.microsoft.com/fr-fr/library/system.windows.controls.contentcontrol(v=vs.110).aspx

Related

Updating values of dynamically created nested UI elements C#

I am new to UWP and C# but I have been tasked with creating a UI that is created dynamically based on an XML file that is read in.
I am having trouble updating values that are nested inside an ObservableCollection.
The current structure uses StackPanels as a container to add UI elements to. The heirarchy looks like this when the elements are created.
StackPanel = Outermost container panel
-StackPanel = Section Stack panel :implements ObservableCollection
--StackPanel = Entry container panel :implements INotifyPropertyChanged
---StackPanel = Entry inner Wrapper panel :implements INotifyPropertyChanged
---- This is where the UI elements like textboxes live
---- TextBox
---- TextBox
I need extract the values of the TextBoxes but they don't ever update from their original value.
I am having trouble finding documentation that points to a good way of doing this kind of thing (usually means it shouldn't really be done haaa) any input would be much appreciated.
You need to do the following:
Read the data from your xml to your ObservableCollection to build a data as base data structure.
Use binding to bind it to your UI elements from your ObservableCollection. By the way, as Peter mentioned, make sure you've already made it twoway binding, in that way the changes from your UI will reflect the data store in memory
Use some ways(for example use a button) to save changes from your UI and then update and save the data in memory to your original xml file.
Some documention can be found from the following:
https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-quickstart
Code Sample:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XmlDocument
I ended up using this link to solve my problem.
http://www.teixeira-soft.com/bluescreen/2016/03/23/c-how-to-dinamicaly-select-a-datatemplate-for-each-item-in-a-list/

Xamarin forms: Display and bind data for list/grid with multiple rows and columns

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).

How could I go about turning this into a user managed area (if possible)?

I have this Groupbox setup in a C# WPF:
Lets say that the company ends up purchasing a new vehicle, how could that company add such vehicle to this window, without me having to add it to the designer like I have, and push out an update? If this is even possible. I feel like there is a better way of doing this.
The data the appears in these Comboboxes are names of drivers.
Create a base class for each such thing, let's say you name it Thing.
Use a ViewModel (if you don't have it yet) with an ObservableCollection<Thing>.
Then, in XAML, use an ItemsControl to define the DataTemplate for each such thing and bind to the ObservableCollection in the ViewModel. (I suggest you use WrapPanel for the ItemsPanel property of the ItemsControl).
That's it! Whenever you add a new Thing to the ObservableCollection in the ViewModel, it will automatically add it to the UI!

WPF Show Data in Grid

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.

WPF check if a dataproperty has an binding

Hej,
I'm trying to make som general functionality for my ListView, so that the content of a ListView can be exported to CSV directly.
I'm trying to achive this by getting the datacontext and analysing the ICollectionView for this. From here I have access to the all the objects from via ICollectionView via SourceCollection, in which I (for now) presume sorting/and filtering is respected.
The challange here is that I only want to output the columns that also are showed in the ListView.
When iterating my collection, is there a function where I can evluate if a property in a class (with notification suppoert) has a binding to it?
The esiest solution for now would be just to output all properties, but I'm not interested in this, since oid's are not fun to look at.
Thx in advanced.
/Ian
I suggest you recognize that the ability to determine which data is being displayed is a business requirement. Thus, you should embody this requirement in your model. In other words, your model should clearly indicate which columns are visible - you shouldn't be trying to infer this from your existing properties, nor should you be examining your view.
There are a whole bunch of ways you could do this, but the key is to have this information on hand in your model.
Why don't you just look at the DataTemplate and evaluate it including the binding inside?
There's no easy way to do it... You can check the binding on the target side (dependency property), but not on the source side.
For what you're trying to do, you could loop through the columns of the ListView and check their DisplayMemberBinding, but it could be undefined (the cell content might be defined using the CellTemplate property instead).

Categories