【问题标题】:Binding with DependencyProperty与 DependencyProperty 绑定
【发布时间】:2012-08-25 20:45:54
【问题描述】:

我定义了一个类名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“标题”的值,而 RichTextBlock 中的 Binding 起作用。

有什么我错过的吗?

【问题讨论】:

  • 你不会用&lt;/ScrollViewer&gt;关闭ScrollViewer

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2015-05-12
    • 2012-07-24
    • 2013-06-03
    相关资源
    最近更新 更多