使用DependencyProperty绑定

时间:2012-08-31 03:21:32

标签: silverlight binding windows-8 microsoft-metro dependency-properties

我已经定义了一个类名TextColumns.cs,它有一个DependencyProperty RichTextBlockContentProperty:

public static readonly DependencyProperty RichTextBlockContentProperty =
    DependencyProperty.Register("RichTextBlockContent", typeof(string),
typeof(RichTextColumns), new PropertyMetadata(""));

public string RichTextBlockContent
{
    get { return (string)GetValue(RichTextBlockContentProperty); }
    set  //Debug, but the SetValue won't fire
    {
        SetValue(RichTextBlockContentProperty, value);
    }
} 

在XAML中,我将其用作

<FlipView x:Name="flipView"
            ItemsSource="{Binding Source={StaticResource itemsViewSource}}">
            <FlipView.ItemTemplate>
                <DataTemplate  x:Name="myDataTemplate">
                    <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                        <ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" Grid.Row="1">
                            <!-- Content is allowed to flow across as many columns as needed -->
                            <common:RichTextColumns x:Name="richTextColumns" Margin="117,0,117,47"
                                                    RichTextBlockContent="{Binding title}">

                                <RichTextBlock x:Name="richTextBlock" Width="560" Style="{StaticResource ItemRichTextStyle}">
                                    <Paragraph>
                                        <Run x:Name="RunText" FontSize="26" FontWeight="SemiBold" Text="{Binding title}"/>
                                    </Paragraph>
                                </RichTextBlock>
                </common:RichTextColumns>
            </UserControl>
        </DataTemplate> 
    </FlipView.ItemTemplate>
</FlipView>

加载页面时,假设RichTextBlockContent将获取Binding“title”的值,而RichTextBlock中的Binding则有效。

有什么我错过的吗?

1 个答案:

答案 0 :(得分:1)

不会调用setter。如果在设置值时需要执行逻辑,则需要在PropertyMetadata构造函数中提供PropertyChanged回调

http://msdn.microsoft.com/en-us/library/ms557330.aspx