Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何绑定文本框和属性?_Wpf_Silverlight_Silverlight 4.0_Binding - Fatal编程技术网

Wpf 如何绑定文本框和属性?

Wpf 如何绑定文本框和属性?,wpf,silverlight,silverlight-4.0,binding,Wpf,Silverlight,Silverlight 4.0,Binding,RoomWidth-是属性 <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmln

RoomWidth-是属性

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
    xmlns:System_Windows_Controls_Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Toolkit"
    x:Class="SilverlightApplication5.MainPage"
    Width="640" Height="480">
    <StackPanel x:Name="LayoutRoot" Background="White">
        <TextBox x:Name="tbWidth" TextWrapping="Wrap" 
           Text="{Binding Mode=TwoWay, ValidatesOnExceptions=True, Path=RoomWidth}"/>
        </StackPanel>
</UserControl>
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
命名空间SilverlightApplication5
{
公共部分类主页面:UserControl
{
公共主页()
{
初始化组件();
}
私人室内空间宽度=10;
公共室内宽度
{
获取{return roomWidth;}
设置
{
如果(值<0 | |值>100)
{
抛出新异常(“数据不正确”);
}
房间宽度=数值;
}
}
}
}

我需要添加到这个类的绑定源。如何做到这一点?

DataContext=this


将其放入构造函数中。

使用
ElementName
例如:

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 SilverlightApplication5
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private int roomWidth = 10;
        public int RoomWidth
        {
            get { return roomWidth; }
            set
            {
                if (value < 0 || value > 100)
                {
                    throw new Exception("Data not correct");
                }
                roomWidth = value;
            }
        }

    }
}


如果您在基本绑定方面有这样的问题,您应该仔细阅读。(/)

信息不足,您甚至没有发布课程标题。您应该提出您自己有信心回答的问题。我不知道你的问题是什么。@H.B和机器人寿司,我更新帖子。
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:System_Windows_Controls_Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Toolkit"
    x:Class="SilverlightApplication5.MainPage"
    Width="640" Height="480"
    Name="control">

    <!-- ... -->
    <TextBox Text="{Binding ElementName=control, Mode=TwoWay, ValidatesOnExceptions=True, Path=RoomWidth}" x:Name="tbWidth" TextWrapping="Wrap"/>