本文介绍了如何将属性绑定到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
推荐答案
<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
这篇关于如何将属性绑定到xaml控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!