如何将属性绑定到xaml控件

如何将属性绑定到xaml控件

问题描述:

我在xaml中有一个文本框,在xaml.cs中有一个属性(例如名字).

现在如何将此属性绑定到xaml textbox

I have a textbox in my xaml and a property (say First name)in xaml.cs.

Now how to bind this property to xaml textbox

查看示例:
Check out a sample:
<usercontrol x:class="SilverlightApplication1.MainPage" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <grid x:name="LayoutRoot" background="White">
        <textblock text="{Binding TextProperty}" height="30" width="200"></textblock>
    </grid>
</usercontrol>


代码:


Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public string TextProperty { get; set; }

        public MainPage ()
        {
            InitializeComponent();
            TextProperty = "This is the Silverlight";
            this.DataContext = this;
        }
    }
}


参考号:将属性绑定到文本块

另请参阅: Binding.ElementName属性


Ref.: Bind Property to Textblock

Also refer: Binding.ElementName Property


Silverlight数据绑定 [到控件的数据绑定:Silverlight.NET [ ^ ]

绑定标记扩展 [
Silverlight Data Binding[^]

Data Binding to Controls: Silverlight.NET[^]

Binding Markup Extension[^]