【发布时间】:2014-10-21 17:08:06
【问题描述】:
我有一个将多个属性传递给内部文本块的用户控件,如下所示:
<UserControl x:Name="Root">
<Grid>
<TextBlock x:Name="ContentBlock"
TextWrapping="WrapWholeWords"
FontFamily="{Binding FontFamily}"
FontWeight="{Binding FontWeight}"
Text="{Binding Text}"
LineHeight="{Binding LineHeight}"
Foreground="{Binding Foreground}"
/>
</Grid>
</UserControl>
在用户控件中我有以下内容:
public CustomTextBlock()
{
this.InitializeComponent();
(this.Content as FrameworkElement).DataContext = this;
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",
typeof(string), typeof(CustomTextBlock), null);
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValueDp(TextProperty, value); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void SetValueDp(DependencyProperty property, object value, [CallerMemberName]string propertyName = null)
{
SetValue(property, value);
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
我在使用这个控件的地方绑定了一些属性,如下所示:
<controls:CustomTextBlock Text="{Binding Question.Text}"
FontSize="30"
MaxHeight="130"
Margin="0, 0, 10, 0"/>
但是,文本没有正确地通过绑定传递到控件中。我所看到的一切都说这应该有效,但事实并非如此。任何想法是什么导致我的文本没有传递到依赖属性?
【问题讨论】:
-
在我看来是正确的...您在“输出”窗口中看到任何绑定错误吗?
-
没有我可以看到的绑定错误,没有。
标签: c# xaml windows-runtime windows-phone-8.1