|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionDatabinding is one of the things that UI developers need constantly. WPF offers some nice things for databinding. Additionally, WPF offers alleviation to some operations that where not difficult, but uncomfortable to do in WinForms. Examples of these techniques were setting window transparency and shape. Now, it's ridiculously easy to make a window (or any UI element) rounded. I'll also throw in a context menu stuff. I think this is useful information for beginner WPF programmers. Hopefully, you enjoy this article! To summarize, we're going to go through the following areas:
Let's see how they operate... Using the codeHere are the use cases for the attached example code:
DatabindingIn our example, the The class Person : INotifyPropertyChanged
{
string name;
public string OriginalName = "Timo";
public string Name
{
get { return name; }
set
{
name = value;
Notify("Name");
}
}
public Person()
{
this.Name = OriginalName;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void Notify(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
#endregion
}
As mentioned earlier, "Two-way databinding" ensures that changes from the UI to propagate to the PersonObject = new Person();
PersonObject.PropertyChanged += new
System.ComponentModel.PropertyChangedEventHandler(PersonObject_PropertyChanged);
this.DataContext = PersonObject;
PersonObject.Name = PersonObject.OriginalName; // set to initial value
In the above code snippet, it's noteworthy to mention about setting the The target for databinding is a textbox: <TextBox Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Width="120"
Height="Auto"
Margin="5"
Name="textBox1"
Background="Yellow"
FontWeight="Bold"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
Loaded="textBox1_MoveCursorToEnd"
GotFocus="textBox1_MoveCursorToEnd"/>
Now, when the content of the When looking at the code, you'll find a strange call to the Moving the cursorMoving the cursor at the end of the text box's current content is done in the Rounded windowIn WPF, rounded windows are easy to do. First, the window-object's TransparencyWindow transparency is simpler to achieve in WPF than in WinForms. It's just a matter of setting the window-object's Context menuContext menus are fun and intuitive to use. The window class has a <Window.ContextMenu>
<ContextMenu Background="LightGreen">
<MenuItem Header="Transparent" Click="TransparencyMenuItem_Click" IsCheckable="True"/>
<MenuItem Header="Close" Click="CloseMenuItem_Click" IsCheckable="False"/>
</ContextMenu>
</Window.ContextMenu>
The above code adds two menu items ( Points of InterestThe process of databinding business objects to UI has become significantly more elegant with WPF. There are some tricks of the trade to be learned, namely the order in which the binding happens in relation to the UI rendering. This must be kept in mind when specific user experience is to be grasped. The best way to learn WPF is to roll up your sleeves and hit your hands into the dirt. This avoids having to hit your head to the monitor when deadlines are closing in. I hope you liked this beginner level article. Feedback is welcome! History
|
|||||||||||||||||||||||||||||||||||