Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4 (Expert's Voice in .NET)

Category: Programming
Author: Matthew MacDonald
4.1
All Stack Overflow 11
This Month Stack Overflow 5

Comments

by anonymous   2019-07-21

For an in-depth learning resource, check out the Pro WPF in C# 2010 book: http://www.amazon.com/Pro-WPF-2010-Presentation-Foundation/dp/1430272058/ref=sr_1_1?ie=UTF8&qid=1339377007&sr=8-1

It includes several chapters on elements and how positioning works. I've found it to be extremely helpful and would recommend it.

If you're not interested in reading something so lengthy, I would recommend viewing the example code on MSDN for different layout controls, such as the Grid, StackPanel, and DockPanel.

by anonymous   2019-07-21

The easiest way i found is taken from this book, pages 624-625.

The ViewModel should implement IDataErrorInfo

private string _newItem;

public string NewItem
        {
            get { return _newItem; }
            set
            {
                if (Equals(_newItem, value)) return;
                _newItem = value;
                SendPropertyChanged("NewItem");
            }
        }

public string this[string propertyName]
        {
            get
            {
                if (propertyName == "NewItem")
                {
                    var valid = NewItem.All(Char.IsLetterOrDigit);
                    if (!valid)
                        return "NewItem can only contain letters and numbers.";
                }
                return null; 
            }
        }

And the view the long version:

<TextBox>
   <TextBox.Text>
      <Binding UpdateSourceTrigger="PropertyChanged" Path="NewItem">
         <Binding.ValidationRules>
            <DataErrorValidationRule></DataErrorValidationRule>
         </Binding.ValidationRules>
      </Binding>
   </TextBox.Text>
</TextBox>

Or the short version:

<TextBox Text="{Binding NewItem,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"/>

It should create a nice red border around your textbox when rule fails, and you can play around with the error message the way you want, for example bind the error message to a textbox tool tip (MSDN):

<Window.Resources>
        <Style x:Key="TextBoxInError" TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
              Value="{Binding RelativeSource={x:Static RelativeSource.Self},
              Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

And then just add this to the textbox:

Style="{StaticResource TextBoxInError}"

Cheers!

by anonymous   2019-07-21
  • Follow the MVVM pattern
  • Don't use code-behind unless you really have to
  • Try and keep all your logic in your ViewModels
  • Try to make your views dumb, so that they only concern themselves with presentation
  • Create standard control styles and templates and push them into resource dictionaries so that most of the XAML in your Views is plain and simple
  • Use (and study) a good WPF Framework such as Caliburn Micro which will push you the direction of best practice
  • Pick up a copy of Pro WPF in C# 2010
by anonymous   2017-08-20

i think u should read good books about CLR/C# (such as this) and more specifically about WPF (i recommend either WPF 4 Unleashed or Pro WPF in C# 2010)

also, u can find nice code samples at WPF samples

And ,finally, u should necessarily code ur own programs: more code - more experience

by anonymous   2017-08-20

Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4
http://www.amazon.com/Pro-WPF-2010-Presentation-Foundation/dp/1430272058

Cover image

It is a very up-to-date book, and I learned WPF from it in 2008. It's for beginners, but covers everything, from "what is user32, what is gdi+ and what is directx" to "how to implement your own plugin system in a WPF app". The only missing part is the MVVM pattern explanation, thought it thoroughly deals with Data-Binding concepts.

Extracted from amazon:

What you'll learn

  • WPF basics: XAML, layout, control essentials, and data flow
  • WPF applications: Navigation, commands, localization, and
    deployment
  • Advanced controls: Custom controls, menus, toolbars, and trees
  • WPF documents: Text layout, printing, and document packaging
  • Graphics and multimedia: Drawing shapes, sound and video, animation, geometric transformations, and imaging
  • (note from me) I would also add Data-Binding as a strong 'plus' of this book

Who is this book for?

This book is designed for developers encountering WPF for the first time in their professional lives. A working knowledge of C# and the basic architecture of .NET is helpful to follow the examples easily, but all concepts will be explained from the ground up.

by anonymous   2017-08-20

Following excerpt from MacDonald's Pro WPF in C# 2010 book, p. 966:

The WPF RichTextBox, like most of the rich text controls that have preceded it, can be a bit sluggish. If you need to hold huge amounts of data, use intricate logic to handle key presses, or add effects such as automatic formatting (for example, Visual Studio’s syntax highlighting or Word’s spelling-checker underlining), the WPF RichTextBox probably won’t provide the performance you need.