How to use C# with WPF - c#

I want to create a UI for my application.
What really confuses me is how to actually do it. I know that Microsoft has introduced WPF.
I have also seen some examples, but what I am not getting is whether or not WPF a seperate language? How can I use C# with WPF?
Lets say I want the user to click on a browse button, select a file and display it's content on the UI. I want to do it using C# while WPF providing the UI, is this possible?
Any good resources for a newbie like me?
EDIT
What i didnt understand, will i use WPF for my UI or windows form for my UI while my actual code is in C# ?
I think i am being lost here ? WPF seems something else that supports c#. is that true ? i thought WPF was only for UI while the actual code would be in C# or VB.
I have taken wpf unleashed but it explains wPF and not how to use C# with it. Atleast in the starting chapters ?
I know i am being dumb here but i am really confused

Yes it is definitely possible. Take a look at this article about Mode-View-ViewModel (MVVM) from Microsoft, which is a good introduction to using Xaml (the markup language for WPF) with C#.

Since you are familar with WinForms I will explain it like this.
WinForms provides the GUI and it is similar to what WPF does.
WPF however mainly uses XAML, a markup language based on xml to design the visual elements. It is a presentation foundation on its own that could be used without XAML but it is certainly a big part of it.
When creating an application it will be either WPF or WinForms you can't combine the two (well easily anyway I know there are a few ways to get around it)
You can interact with WPF the same way you interact with WinForms elements.
You can create a Window. On there you can place a grid, where you can then place controls such as a button or textbox. Then in the code behind it is exactly the same as referencing a control in winforms. for example in the page_load function doing
txtInput.Text = "A String";
So what should you choose? (Please note I am about to give you a few things to compare I realize this hardly describes both technologies to its fullest)
WPF
Pros
- Great for visually appealing designs
- You have XAML based control over your visual elements. Meaning you can change the way your form looks by writing xaml instead of doing all through the visual studio IDE pressing buttons.
Cons
- Bit of a performance hog. It has come along way with .net 3.5 sp1 but still chews up quite a few resources
- Not as many controls built for it as WinForms, mainly because its a newer technology.
- Can become complex as syntax for binding information to controls doesn't include intellisense.
WinForms
Pros
- Familiar and well used technology so your development will be faster
- Better performance
- More controls built for it
Cons
- More effort to build a visually appealing design
- Its not new and exciting so to speak. I know we all like to learn new things :)

It is not a seperate language. WPF employs XAML, a derivative of XML, to define and link various UI elements. As in web development, both layouts and specific themes are well suited to markup, but XAML is not required for either. Indeed, all elements of WPF may be coded in a .NET language (C#, VB.NET). The XAML code can ultimately be compiled into a managed assembly in the same way all .NET languages are.
You can implement your requirement in easily WPF.
To get a hands-on in WPF start with this article, http://10rem.net/blog/2010/02/09/getting-started-with-wpf--hello-world-in-multiple-flavors
Happy coding..

WPF is part of .NET framework, so it's not related with specific programming language. Please read MSDN's "Introduction to WPF" - http://msdn.microsoft.com/en-us/library/aa970268.aspx

Related

Migrating from Winforms to WPF

We have an old application built on .Net 1.1 Framework and Winforms. We are planning to migrate it to the latest version of .Net.
Here are some of the questions running in my mind:
Should we go with Winforms or WPF?
We want the design and structure to the exact same as what we have now.
What kind of pitfalls can I expect when migrating?
Is there any place where I can lookup to see the process of migration?
Any other suggestions?
Should we go with Winforms or WPF?
This is a very broad question. But my answer to that would be this. Winforms designer is a painful experience and it is not as flexible as WPF by any means so if that is important to you then you should.
We want the design and structure to the exact same as what we have now.
Anything you do in Winforms you can replicate in WPF so no worries there.
What kind of pitfalls can I expect when migrating?
There are too many to name one of the most common ones for our migration was converting existing logic in the WinForms (yes it was a crappy, outsourced, legacy WinForms full of business logic) and connecting it to WPF elements. The process in and of itself is not that complicated but when you have too many intertwined pieces it can get pretty ugly.
Is there any place where I can lookup to see the process of migration?
The process of migration pretty much has to be iterative. So the way many people do it based on my research (articles, StackOverflow answers and such) is through ElementHosts.
1) Target a certain part of your WinForms that you would like to switch up to WPF and then take it out.
2) Replace it with ElementHost.
3) Then in that ElementHost you will have your newly rewritten WPF counterpart.
4) Test it out make sure that it works okay with the rest of the elements.
5) Go to step 1 until rest of the stuff on that window is replaced (You can do header, middle, footer or top, bottom any way you wish to go about replacing elements on the window it depends on your particular situation).
6) Once most of the stuff is replaced you can then combine all of those WPF User Controls into a WPF Window.

How can I create/skin a C# UI?

Ive been working on a C# project for a while now and I fell it needs change its look.
I have found a UI kit that I really like, which can be seen below:
http://medialoot.com/item/transparent-ui-kit/
Does anyone know how to do this?
Thanks
EDIT: Im using WinForms
EDIT2: Maybe I should convert over to WPF? Is this doable?
You can subclass most common controls and draw their appearance yourself. However, unless for novelty applications I doubt your users will thank you for doing so.
Your example is a library of controls. For you to have an appearance such as that you'll have to create or acquire a similar library of controls and replace all your controls in your project to get that appearance.
If you want to update colors (background, foreground, etc.) and such you can make a class that recurses through controls and sets the settings using reflection and a switch statement to process each control. Then just run this on each form before you show it. One word of caution about this, some controls don't respect your changes and get overridden with themes from the OS (datetimerpicker being the biggest culprit). You'll also need to consider whether your users will appreciate the extra work put in for color/appearance changes.
If you don't have very good design skill and have a good know how about creating such templates, its better to buy them.
And C# is just a language. You're looking to create templates and skins for either for ASP.NET website/application or for windows forms.
Have a look at DevExpress Skins
If you can afford it.

Are there no built in C# GUI Layouts?

I'm used to the GUI frameworks in Java as well as the QT GUI framework, and I'm used to the various layout managers. It doesn't seem that C# has any layout managers built in, or am I missing something?
2 Year Later Edit
I just want to point out to any readers of this question that in hind-sight, my question was misplaced. With proper anchoring and docking of child controls, having a need for the layout managers of Java and QT4 is nearly non-existent.
WPF does have layout managers, see:
http://msdn.microsoft.com/en-us/library/ms745058.aspx
If you're coming from a Java background, then the section "Panel Elements and Custom Layout Behaviors" will be of particular interest.
Both WPF and Windows Forms provide layout capabilities (WPF is just much better at it).
You can achieve moderately complex layouts in Windows Forms too by utilizing the Dock and Anchor properties of controls. Personally I learned what can be achieved and how through Petzold's book on WinForms. If you don't have access to that book, read this short article.
chibacity is right, WPF is loaded with layout managers, all of them are very good.
However if you're going down the WinForms route, you're stuck with TableLayoutPanel, FlowLayoutPanel and SplitContainer. As well as the usual manual Panel and GroupBox controls.
No, you are not missing anything.
.NET does not have built in layout managers for Winforms/Webforms/Console development.
Probably because Visual Studio has good designers, obviating the need.
WPF and Silverlight (both using XAML) do have them, though they are not exactly the same as the Java ones.
WPF and Silverlight have different "Panels" that work similar to the layout managers in other languages.

how to design a calendar control

I need to design a calendar control which should be added to our companie's application (I know there's already quite a lot calendar controls but I shall develop our own one...).
How should I start, should I use a kind of table to display the days or should I completely draw my own grid? How can I do this (I do not need rdy-to-use code, I just need some ideas...)
The application is written in C# as a WindowsForms application (thanks for the hint, forgot to mention this in first case...)
Seeing your comment about WinForms and:
I need to develop an own one because it must be integrated in an already existent application, I need full access in means of style and functionality
makes me suggest to use ready project http://www.codeproject.com/KB/selection/MonthCalendar.aspx and modify it if necessary. I use it in my own little project and it works like a charm. It provides full source if necessary so you can integrate it easily and modify if you think it's not fit enough.
In the end if you end up not using it, you can peak at the sources and functionality it implements and do it your own way.
To me redoing it from scratch is a bit pointless especially with such a good / free one.
If you are developing a web application then I would seriously look at using jquery. A good calendar control should be done client side so I would be looking at some form of java script solution. If you look at jquery-calendar.js as a good example you will see the complexity involved in developing your own control.

Is XAML in WPF equivalent of .Designer.cs in Windows Forms apps?

Is XAML in WPF equivalent of .Designer.cs in Windows Forms apps?
Does it just provide compile-time state for the UI?
I am not sure but it looks like you can do things programmatically with XAML at run-time.
If I have a basic UI state where everything is added at run-time, then should I be looking outside the XAML stuff?
It's probably safe to look at XAML that way - although it's not entirely accurate. The XAML is compiled into BAML, and parsed at runtime - where the Windows Forms designer.cs file is just another C# file built by the designer. XAML is never directly translated into C#.
You can do everything done in XAML via code, though. Charles Petzold's WPF book actually takes this approach. It builds entire WPF applications in code before he ever introduces XAML.
In addition to Reed's input, I should mention that:
XAML uses XML syntax to define UI, but Form does not.
In XAML, it is much easier to separate UI from the logic by using code-behind-design. As UI is only XML, and they can be designed totally independent of any code-behind changes.
Minor changes like Button where you can set Content instead of Text, so you can set anything whereas it could be only text.
Data Binding and Dependency properties are way easier and better in WPF comapring to Windows.Form
Very easy and cool way to handle your design, as you can find there are many cool examples on the net.
You can use tools such as Snoop to visualize your 3D design, and can inspect and verify all your controls and events by selecting the UI. You can see what events are handled or not. This is a very cool tool, and you can find similar ones.

Categories

Resources