views:

75

answers:

2

Using Expression Blend ?

Let's say I got:

Public string FirstName{get;set;}

Edited : 23/8/2010

Thanks for tha answers, But I'm Afraid people didn't understand my question.

I do know how to Bind Data in Code or in XAML My question is if there is a way to do all that with the Expression Blend Interface without writing it directly. Only with mouse movements.

A: 

Please don't down-vote this answer (again) unless you go the the trouble to read the original ambiguous question before it was edited!

You would actually want to put the property on a View Model, and use XAML binding, but that is another story.

As you describe your example, you would first need to implement the "FirstName" property as a Dependency Property and not a simple get/set. Here is a great code-snippet from Shawn Wildermuth to save lots of typing (there is a single typo in the snippet you need to fix - "($type$)args.NewValue;"... NewValue has the wrong case in the snippet).

You can bind in XAML to a simple get/set property, but it is a one-way/one-time binding and will not update with changes.

In code, the binding requires two things to be set.

  • Set the DataContext of the control (or the page) and
  • Set a data binding on the control.

For the example you mention you could use code like the following (assumes a TextBox control called myTextBox in the Xaml):

using System.Windows;
using System.Windows.Controls;

namespace BindingCodeTest
{
        public partial class BindingCode : UserControl
        {
            public string FirstName
            {
                get { return (string)GetValue(FirstNameProperty); }
                set { SetValue(FirstNameProperty, value); }
            }

            // Using a DependencyProperty as the backing store for FirstName.  
            // This enables animation, styling, binding, etc...
            public static readonly DependencyProperty FirstNameProperty =
                DependencyProperty.Register("FirstName",
                                            typeof(string),
                                            typeof(BindingCode),
                                            new PropertyMetadata(string.Empty,
                                            new PropertyChangedCallback(OnFirstNameChanged)));

            static void OnFirstNameChanged(object sender, DependencyPropertyChangedEventArgs args)
            {
                // Get reference to self
                BindingCode source = (BindingCode)sender;

                // Add Handling Code
                string newValue = (string)args.NewValue;
            }

            public BindingCode()
            {
                InitializeComponent();
                myTextBox.DataContext = this;
                myTextBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("FirstName"));
                FirstName = "First name";    // Sample change
            }
        }
}
Enough already
+2  A: 

In Blend 4, on the 'Data' tab > New sample Data.. > name data source as you like, f.e. 'MySampleDataSource'. Then your 'MySampleDataSource' will have a '+' button (the same Data tab on the right) with 3 options. Choose 'Add simple property' and name it 'FirstName'. Then drag that property on your TextBox or TextBlock.

The result is like this:

<TextBlock x:Name="firstName" Text="{Binding FirstName}"/>
VyvIT
I guess that's the closest solution for my isue
Erez