如何在 CodeBehind 中将文本框数据绑定到属性

发布于 2024-09-15 03:18:36 字数 239 浏览 3 评论 0原文

我正在使用Expression Blend。

假设我得到:

Public string FirstName{get;set;}

编辑:感谢您的回答,但恐怕人们不理解我的问题。我确实知道如何在代码或 XAML 中绑定数据。

我的问题是,是否有一种方法可以使用 Expression Blend Interface 完成所有这些操作,而无需直接编写它。仅通过鼠标移动。

I am using Expression Blend.

Let's say I got:

Public string FirstName{get;set;}

Edit: thanks for the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟到的我 2024-09-22 03:18:36

您实际上希望将该属性放在视图模型上,并使用 XAML 绑定,但这是另一回事了。

当您描述示例时,您首先需要将“FirstName”属性实现为依赖项属性,而不是简单的获取/设置。 这是 Shawn Wildermuth 提供的一个很棒的代码片段,可以节省大量的打字时间(有您需要修复代码片段中的一个拼写错误 - “($type$)args.NewValue;”...NewValue 在代码片段中的大小写错误)。

您可以在 XAML 中绑定到简单的获取/设置属性,但它是单向/一次性绑定,并且不会随更改而更新。

在代码中,绑定需要设置两件事。

  • 设置控件(或页面)的 DataContext 并
  • 在控件上设置数据绑定。

对于您提到的示例,您可以使用如下代码(假设 Xaml 中有一个名为 myTextBox 的 TextBox 控件):

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
        }
    }
}

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
        }
    }
}
゛清羽墨安 2024-09-22 03:18:36

在 Blend 4 中,在“数据”选项卡上 >新样本数据..>>根据您的喜好命名数据源,如“MySampleDataSource”。然后,您的“MySampleDataSource”将有一个“+”按钮(右侧的“数据”选项卡相同),其中包含 3 个选项。选择“添加简单属性”并将其命名为“FirstName”。然后将该属性拖动到 TextBox 或 TextBlock 上。

结果是这样的:

<TextBlock x:Name="firstName" Text="{Binding FirstName}"/>

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}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文