簡體   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