繁体   English   中英

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

[英]WP8.1/WinRT User Control Binding from Bound Dependency Property

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

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

但是,文本没有通过绑定正确传递到控件中。 我所看到的一切都说这应该起作用,但事实并非如此。 任何想法导致我的文本不传递到依赖项属性吗?

您的控件的类型为CustomTextBlock但该属性定义为typeof(ShrinkingTextBlock)所拥有。 这些需要匹配才能进行绑定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM