C# WPF Multiple Panels in App - how to do that - c#

I'm pretty new to WPF/XAML in C#, I saw some simple tutorials and stuff but, today I'm confused, never used to make UI there.
I need to output multiple forms which instance would look something like that:
(NB: I constructed it just in WYSIWG way, so XAML markup is total mess, it for screenshot. I cant even add table directly in grid, oh)
I got data like this for each month in schoolyear, so, depends on what class is choosen I want it to output like that (scheme)
(NB: instead of each CustomWindowInstance there I need my form from 1 st screenshot)
So, whats is best and simplies approach for that?

EDIT 1: I'll name XAML from the first screenshot, which represents controls you want to display multiple times, XAML 1, and another one, where you want to do it, XAML 2.
--
Almost ORIGINAL:
Create a separate User Control and move the content of Window tag from XAML 1 into it. Then you'll be able to show it as many times as you want by placing <yourUserControlNamespace:YourUserControlClass /> in XAML 2.
For an ability of communication with this control in XAML like <yourUserControlNamespace:YourUserControlClass Parameter="SomethingHere" /> you should implement a dependency property with name ParameterProperty (desired name of property in XAML + "Property") in YourUserControlClass.xaml.cs.
--
EDIT 2: One may also create a DataTemplate with parameter DataType={x:Type yourVMNamespace:YourVMClass} for the XAML 1's ViewModel (not View) in XAML 2's or the whole application's Resources. In that case the ViewModel class you are binding to will be represented by the DataTemplate's content itself. Later you can write <yourVMNamespace:YourVMClass /> as in the first solution.
This approach allows displaying rich representation of any items themselves, for example, in controls derived from ItemsControl, such as ListBox, all you've got to do is to bind ListBox's ItemsSource property to a collection of YourVMClass instances (or specify them in XAML by hand).
More precisely, in this case I won't call YourVMClass a ViewModel.

Related

XAML based on Conditions (IF/Then View)

So I am trying to build a usercontrol that may or may not have children to it. I know C# but still learning a lot about the XAML side of things.
What I want to do is something like:
IF Children.Count > 0 then Create TreeView
ELSE Create Label
I know how to create a single view but I don't know how to create a view based on conditions like this. Thanks for the help!
A "no Code questions" is rewarded by a "no Code answer" ;o)
Put a treeview in your "usercontrol"
Put your Label in your "usercontrol"
Bind the Visibility property of both to your Collection Holding your "Children".
Create two different (or one parametrized) IValueConverter-Implementing-Converter and use that as "Converter=...." in your Binding.
Make the one converter return Visibility.Hidden when you have less than two childs, make the second return Visibility.Hidden when you have zero or one child.
Put the correct Converter to each of your Bindings so the Label is Hidden when more than one Child is in your Collection and the TreeView is hidden if less than two Childs are in the Collection.
Rethink Building a full fleshed "usercontrol" for this ...

External UserControl with Bindings in WPF/C#

So, I have an UserControl which is basically a Grid with 3 different DataGrids and some Labels. Seeing how I need to use this 3 times, instead of copying and pasting the code, I thought I'd just generate it once and use it in my main window.
I have defined the UserControl as:
<UserControl x:Class="Propuestas.UI.Andrei.DGMTX"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Propuestas.UI.Andrei"
mc:Ignorable="d"
Height="300"
Width="791.496">
And I am using it in my window as such:
<StackPanel Grid.Row="2">
<local:DGMTX/>
<local:DGMTX/>
<local:DGMTX/>
</StackPanel>
For some reason, it doesn't show up in the designer panel on my main window. Is there something I'm doing wrong?
Also, I would like to be able to bind based on a bound element. For example, let's say I have a class Model which has all the data that I need to represent in my UserControl.
I would like to do something like
<local:DGMTX Binding = {Binding Model}/>
and then be able to bind all of the other elements in my UserControl in its code. Is there a way I could do this in XAML? Or do I have to do it programmatically?
There are two ways to communicate your view model to controls:
As one commenter suggested, bind your view model to the data context of the user control. This enables binding everything in your view model to the inner workings of the control. Problem is the inner workings now depend on the data the object is associated with.
Create dependency properties for only the ones in your view model that the user control actually needs. I personally prefer this over the first in almost 99% of all cases because you know exactly what data the control expects and you can manipulate bound data in ways unique to the control that maybe the view model isn't responsible for.
Couple things to note about designer support when creating your own controls:
Visual Studio's designer still has a lot of issues when it comes to WPF. Don't believe me? Try referencing a dynamic resource defined in your main assembly in another. The designer will crash and tell you it can't be found. This isn't the actual case, however. As soon as you run the app, you will never see this exception.
In order to see changes made to source reflect in designer, you have to build the project first (the project in which the control resides, not necessarily the one it's referenced in). Sometimes, "cleaning" or (with better luck in some cases) "rebuilding" the project is the only thing that updates the designer in the main project when "building" doesn't work.
If after considering the latter and you still can't see anything, consider the implementation of the control. Is anything out of place? Did something accidentally get hidden? You may not think so at first and maybe it takes ten hours of frustration to succumb and check, but the little things can make all the difference.

How to make List<Object> variable appear in DataBinding UI when using WPF in Visual Studio?

I'm in the process of learning WPF and currently exploring data binding. I have a DataGrid control on my form, and in my C# code for the form I have a List<string> variable.
I want to be able to use the Properties UI for the DataGrid in the designer to bind the List<string> to the DataGrid. I cannot figure out what I need to do or where I need to look in the UI to do this.
This is what I am doing:
Click my DataGrid in the UI designer to make it the active control.
Move to the Properties window.
Scroll down to the ItemsSource property.
Click on the field and the UI with Source, Path, Converter and Options pops up.
And when I get to this point I no longer know what to do.
I do not want to accomplish this by writing/modifying XAML. I want to know how it works using the UI.
Having never used the designer before, I can't be totally sure (your use case isn't quite clear either).
That being said, in my designer you
Set the "Binding Type" to "Data Context"
Select the "Custom" text box (needed for me because it doesn't see my DataContext)
Type the name of your property in the "Path" field (you can only bind to Properties)
Hit OK.
Note that this is the same as writing in XAML:
<DataGrid ItemsSource="{Binding MyItemCollection}"/>
<!-- or --!>
<DataGrid ItemsSource="{Binding Path=MytItemsCollection}"/>
There's a reason no one uses the designer....
The other options are more "advanced" binding concepts that you don't normally use on ItemsSource properties.
Note that DataGrid is a poor choice for displaying strings. ListView or ListBox are much better choices, as they don't assume your information has multiple pieces (like DataGrid does).
I understand not liking XAML, as it really intimidated me at first, but I will quickly say that it is a powerful tool. I am not sure how to do it through the designer, but in C# let's say you name your DataGrid 'myDataGrid' and your List is named 'stringList'. It is as simple as the following:
myDataGrid.ItemsSource = stringList;
and the data grid is now bound to your string list.
Thanks for asking the question! The properties window is so underrated.
First you must set the DataContext.
It's in the common section of the properties window. Set the data context to whatever view model you need. If you don't have a VM and the List is in the code behind, set the data context to relative source self.
Next in the Path write the name of your List.
Also, you may want to use ObservableCollection instead of List so your objects are updated in the UI as they change.

class concept in WPF

I'm new in WPF and I need to group many components in one element and make and add new instance of that element in window for each student in database like 2 textblock plus 1 textbox for each student, how can i do something like that?
This is where WPF really shines - you can use an ItemTemplate or a DataTemplate to style the UI with the underlying data objects knowing absolutely nothing about how they are being presented.
Check out Data Templating for an introduction. Effectively an ItemTemplate is a template (definition) of how each item should be rendered. A DataTemplate goes a step further and gives you the ability to select which template to use based on the data item being bound to, so you can have a list containing different types of objects yet still show them all in the same list/repeater control on the screen.

Extracting ListBox to separate file

So, i have quite complex ListBox, with own style and composite item templates (in every item, there are image and text, both with click events, which rises corresponding commands in global viewmodel). What would be the easiest way to re-use that ListBox?
Should i extract style only, or move it out as UserControl, or should i make new component? Everything is static (commands, markup) exept content (should be able to use different items lists).
In the perfect case, is should be (pseudocode):
Page 1 xaml
<MyListBox Content={Binding Page1List}>
Page 1 vm
ObservableCollection<Items> Page1List;
and so on for other pages.
EDIT: i found this article - that makes sence, but i think there should be easier way for that.
http://www.codeproject.com/Articles/14340/Creating-a-look-less-custom-control-in-WPF
I believe the solution you are looking for is a look-less control.

Categories

Resources