Binding vs manually updating in WPF

What is better to do:

Binding

XAML

<TextBlock Text="{Binding}" Name="txtValue" />

Code behind:

public char Value {get;set;}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    txtValue.DataContext = Value;
}

.

OR - Dependency Property (Manual Update)

XAML

<TextBlock Text="" Name="txtValue" />

Code Behind:

public char Value
{
    get {
        return (char)GetValue(ValueProperty);
    }
    set {
        SetValue(ValueProperty, value);
    }
}
public static DependencyProperty ValueProperty = DependencyProperty.Register(
        name: "Value",           // Name of property
        propertyType: typeof(char), // return type of property
        ownerType: typeof(CharacterControl),  // who is the owner  

        typeMetadata: new FrameworkPropertyMetadata(
            defaultValue: '0', // default value of property
            propertyChangedCallback: ValuePropertyChanged
        )
);
// event to fire when property changes
public static void ValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    CharacterControl control = d as CharacterControl;

    control.Value = (char)e.NewValue;        

    control.txtValue.Text = control.Value.ToString();  // update value in here <--------------
}

I like the second option better because I can see the value meanwhile on edit mode. The first approach I have to compile it for the view to display the correct value. But I think the first option is more efficient? **I will update the value of the textblock very fast and I dont know what approach to use.

Answers


The first option is probably better in most cases because it's simpler.

If you need to be able to see a value in design mode (what I think you mean by "edit mode"), you can set a design-time DataContext using the d:DataContext= attribute in XAML.

For more information, see a page like this one.


Need Your Help

Tips for Project Euler Problem #78

algorithm language-agnostic

This is the problem in question: Problem #78

Is there an easy way to load an external header on all pages using HTML?

jquery html css

I am working on a site with 20+ pages and updating the main links on all pages becomes quite tiresome. I have been able to do this using PHP on a Wordpress template I created a while back, but the