来自绑定依赖属性的WP8.1 / WinRT用户控件绑定

时间:2014-10-21 17:08:07

标签: c# xaml windows-runtime windows-phone-8.1

我有一个用户控件将多个属性传递给内部文本块,如下所示:

<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"/>

但是,文本没有正确地通过绑定传递给控件。我所看到的一切都说这应该有效,但事实并非如此。是什么原因导致我的文本不能传递给依赖属性?

1 个答案:

答案 0 :(得分:0)

您的控件属于CustomTextBlock类型,但该属性已被typeof(ShrinkingTextBlock)所有。这些需要匹配绑定才能工作。