# [.net] Getting started with WPF

This topic is 3265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! I've been wanting to learn WPF since some time and now that I have a week off, I've decided to attempt to create a basic hexadecimal viewer and editor. I've been trying to accomplish something yesterday but I realized that WPF is indeed quite different from Windows Forms. I've managed to create a menu in my window and that's about it :). I don't really know which control (or Element, whatever) to use in order to create the main [Address Offset, Byte 0, Byte 1, ...] table that you usually see in hex editors. I've tried Grid, UniformGrid and attempted to use TextBlocks and FlowDocuments without satisfying results. I need a control that will space the text between each byte columns but allow text selection across them. Any ideas what I should use? Also, are there any must-read or must-see resources out there on getting started with WPF? Any suggestion will be helpful, thanks!

##### Share on other sites
With a Grid, you can specify rows and/or columns with whatever dimensions that you want:

<Grid>   <Grid.ColumnDefinitions>      <ColumnDefinition Width="32" /> // Creates a column of width 32      <ColumnDefinition Width="auto" /> // Creates a column of whatever width is required by its child elements      <ColumnDefinition Width="*" /> // Creates a column of width of whatever width is left   </Grid.ColumnDefinitions></Grid>

However, ListBox is most likely what you want, since you're probably going to want to specify any number of rows. You can set up a template for how each new object inserted into the row will be formatted. I don't know how, off hand, but you can just google it.

Alternatively, if you don't want all of the extra stuff that comes with a ListBox (selection, hover-over color changes, scrollbar, etc.), you can just use a StackPanel, and popuplate it with a set of user controls that are formatted how you want for the display. Then, in code, you just create these user controls, and add them to the StackPanel's Children property. That's probably what I'd do, though data binding might be a better fit.

I hope that helps!

##### Share on other sites
Quote:
 Also, are there any must-read or must-see resources out there on getting started with WPF?

As far as that goes.... play! You can do a lot of cool stuff with WPF, and when you get good, it's really easy to create a great looking UI. My suggestion, once you get the layout basics down is to look at animation (System.Windows.Media.Animation, I believe). It's really easy to animate any number of types. Most often you'll be using DoubleAnimations, but you can do any number of others, and it's even possible to roll your own (though I've never done it). Once you get the hang of that, you'll be animating anything and everything on your UI that has any sort of transition, and it'll be super easy to do.

And, as an added bonus, once you know WPF, you pretty much also know Silverlight. I entered my current job never touching Silverlight, but within a couple of days I'd essentially completely taken over the all Silverlight UI just because of what I knew of WPF. They're very similar (though WPF is nicer, obviously).

##### Share on other sites
Thanks a lot for your feedback, CyberSlag,

I'm going to try the ListBox and StackPanel right now to see if they fit my use case. There's only one thing that's still worrying me: if I use one of those "layout containers" (I still have to learn proper WPF terminology), and embed a number of Labels (I think that's what you're suggesting), how will I make it possible to select text from multiple adjacent labels simultaneously? For example, if I use a StackPanel and have Labels for each byte value (as is usual in hex editors), will I be able to make a selection that spans an arbitrary number of bytes even if they are individual labels?

##### Share on other sites
Well, you could do it a number of ways. You could just use a text box, and modify its control template using Expression Blend. I don't know enough to tell you exactly how to do it, or even that it's fully possible, but I think it would be.

Alternatively, you could handle the mouse events to facilitate selection. Clicking the mouse selects the byte that's currently under the mouse as your FirstSelectedByte, and, more importantly, sets a Selecting flag, such that every time you MouseEnter over a byte, you select everything from the FirstSelectedByte to the sender byte. You'll have to manually facilitate what that means, whether it's a rectangle of selected bytes, or selection as through it was a 1-dimensional array of bytes (even though it's represented 2-dimensionally).

##### Share on other sites
Quote:
Original post by CyberSlag5k
Quote:
 Also, are there any must-read or must-see resources out there on getting started with WPF?

As far as that goes.... play! You can do a lot of cool stuff with WPF, and when you get good, it's really easy to create a great looking UI. My suggestion, once you get the layout basics down is to look at animation (System.Windows.Media.Animation, I believe). It's really easy to animate any number of types. Most often you'll be using DoubleAnimations, but you can do any number of others, and it's even possible to roll your own (though I've never done it). Once you get the hang of that, you'll be animating anything and everything on your UI that has any sort of transition, and it'll be super easy to do.

And, as an added bonus, once you know WPF, you pretty much also know Silverlight. I entered my current job never touching Silverlight, but within a couple of days I'd essentially completely taken over the all Silverlight UI just because of what I knew of WPF. They're very similar (though WPF is nicer, obviously).

If you already know C# and are familiar with the old .Net controls XAML is really the only major change from winforms so it shouldn't be too different.
Petzold latest Programming for Windows book covers all the nitty gritty detail you need for WPF programming but is lacking in pictures compared to other books.
Actually, now that I think about it he probably has the program you are trying to make in that book since I know the first program Petzold likes to write for any new language is a hex editor [lol]
If you are more of a visual person like myself though they got plenty of WPF videos here.

• 9
• 11
• 21
• 10
• 14