【发布时间】:2013-11-06 20:47:37
【问题描述】:
我有两个关于在 Windows Phone 上开发的问题:
我想创建自定义控件并能够在其中提供一些额外的 XAML。所以我在ControlTemplate 中使用ContentControl 和ContentPresenter。
<ContentControl>
<ControlTemplate>
<TextBlock Name="TextBlockControl" Text="Existing controls"/>
<ContentPresenter/>
</ControlTemplate>
</ContentControl>
它有效,但我无法从代码隐藏中访问 ControlTemplate 内的 TextBlockControl。 FindName 总是返回 null。
其次,我想为 Control 提供属性,所以我这样创建 DependencyProperty:
public string CustomText
{
get { return (string)GetValue(CustomTextProperty); }
set
{
SetValue(CustomTextProperty, value);
TextBlockControl.Text = value;
}
}
public static readonly DependencyProperty CustomTextProperty =
DependencyProperty.Register("CustomText", typeof(string), typeof(MyControl), null);
如您所见,我写TextBlockControl.Text = value; 为我的控件内的TextBlock 设置文本。当我设置静态字符串时 - 它可以工作
<MyControl CustomText="Static Text"/>
但是当我想使用 Binding(例如 LocalizedStrings 资源)时 - 它不起作用。我是否缺少 PropertyMeta 回调或某些 IPropertyChanged 继承?我已经阅读了大量关于同一问题的 StackOverflow 问题,但没有一个回答我的问题。
【问题讨论】:
标签: c# xaml windows-phone