无法可靠地绑定到附加属性作为源

时间:2016-02-15 13:13:42

标签: c# windows-runtime attached-properties windows-10-universal

为什么绑定适用于个人TextBlock但不适用于TextBlock内的Button.Flyout

<RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <RichTextBlock Name="rtbMain" attc:RichTextBlockAttached.CurrentFlyoutText="Trial Attached Binding" RelativePanel.AlignHorizontalCenterWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True">
        <Paragraph>
            Button Flyout should display Trial Attached Binding
        </Paragraph>
    </RichTextBlock>
    <Button x:Name="btt" RelativePanel.Below="rtbMain" RelativePanel.AlignHorizontalCenterWith="rtbMain" Content="Click to test binding">
        <Button.Flyout>
            <Flyout>
                <Grid>
                    <TextBlock Text="{Binding Path=(attc:RichTextBlockAttached.CurrentFlyoutText), ElementName=rtbMain, Mode=TwoWay}"/>
                </Grid>
            </Flyout>
        </Button.Flyout>
    </Button>
    <TextBlock RelativePanel.Below="btt" RelativePanel.AlignHorizontalCenterWith="btt" Text="{Binding Path=(attc:RichTextBlockAttached.CurrentFlyoutText), ElementName=rtbMain, Mode=TwoWay}"/>
</RelativePanel>

和附加属性定义:

    public static class RichTextBlockAttached
{
    public static readonly DependencyProperty CurrentFlyoutTextProperty =
        DependencyProperty.RegisterAttached(
            "CurrentFlyoutText",
            typeof(string),
            typeof(RichTextBlockAttached),
            new PropertyMetadata("", OnCurrentFlyoutTextChanged));

    private static void OnCurrentFlyoutTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //throw new NotImplementedException();
    }

    public static string GetCurrentFlyoutText(DependencyObject obj)
    {
        return (string)obj.GetValue(CurrentFlyoutTextProperty);
    }

    public static void SetCurrentFlyoutText(DependencyObject obj, string value)
    {
        obj.SetValue(CurrentFlyoutTextProperty, value);
    }

0 个答案:

没有答案