作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Expression Blend。
假设我得到了:
Public string FirstName{get;set;}
编辑:感谢您的回答,但恐怕人们不明白我的问题。我确实知道如何在代码或 XAML 中绑定(bind)数据。
我的问题是,是否有一种方法可以使用 Expression Blend Interface 完成所有这些操作,而无需直接编写它。仅通过鼠标移动。
最佳答案
您实际上希望将属性放在 View 模型上,并使用 XAML 绑定(bind),但这是另一个故事了。
在描述示例时,您首先需要将“FirstName”属性实现为依赖属性,而不是简单的获取/设置。 Here is a great code-snippet from Shawn Wildermuth节省大量输入(您需要修复代码片段中的一个拼写错误 - “($type$)args.NewValue;”...NewValue 在代码片段中的大小写错误)。
您可以在 XAML 中绑定(bind)到简单的获取/设置属性,但它是单向/一次性绑定(bind),并且不会随更改而更新。
在代码中,绑定(bind)需要设置两件事。
对于您提到的示例,您可以使用如下代码(假设 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
}
}
}
关于wpf - 如何将文本框数据绑定(bind)到 CodeBehind 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3542583/
我是一名优秀的程序员,十分优秀!