【发布时间】:2010-10-07 03:35:57
【问题描述】:
我有一个名为 Field 的自定义用户控件:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Name="LblName"/>
<TextBox Grid.Row="0" Grid.Column="1" HorizontalContentAlignment="Left" Name="TxtValue"/>
</Grid>
在 CS 文件中,我公开了以下属性,它只是设置文本框的“.text”值。
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value",
typeof(string),
typeof(Field),
new PropertyMetadata(
default(string),
(source, args) => (source as Field).TxtValue.Text = (string)args.NewValue
)
);
[Category("Custom")]
public string Value
{
get
{
return (string)this.GetValue(ValueProperty);
}
set
{
this.SetValue(ValueProperty, value);
}
}
第一个问题。这是我们传播子用户控件依赖属性的正确方法,通过父用户控件进行备份,以便可以从页面 xaml 进行设置?我在网上浏览了一下,考虑到这是编写自定义控件时相当原始的事情,所以没有太多内容。
提供有关大图的详细信息,我有客户详细信息页面,一些控件绑定到包含在可观察集合中的客户对象。如果我将页面上的文本框(包含在字段控件中)的值从“xxxx”更改为“yyyy”,我总是会返回“xxxx”。似乎 getter 周围的某个地方出了点问题?
【问题讨论】: