【问题标题】:What is an alternative to using RelativeSource Self?使用 RelativeSource Self 的替代方法是什么?
【发布时间】:2026-02-16 20:45:01
【问题描述】:

在 Xamarin Forms 中,我不确定使用 RelativeSource Self 的最佳选择是什么。

例如,如果我想要一个标签绑定到它自己的 Text 或 Tag 属性,那么在 WPF 中我可以这样做:

    <Style TargetType="TextBlock">
        <Setter Property="Text">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource TextConverter}">
                    <Binding RelativeSource="{RelativeSource Self}" Path="Tag" />
                    <Binding ElementName="Window" Path="DataContext.SelectedContent" />
                    <Binding ElementName="Window" Path="DataContext.CopyMade" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>

使用 Xamarin.Forms 的最佳替代方法是什么?

【问题讨论】:

    标签: xaml xamarin.forms


    【解决方案1】:

    Xamarin.Forms 不支持RelativeSource,通常的替代方法是使用Source={x:Reference} 标记扩展。

    根据您的样式是否被定义为ResourceDictionary 中的资源,因此可以重复使用,您可能会也可能无法使用{x:Reference} 作为替代方案。

    【讨论】: