1

需要在 XAML 中执行此操作:

<Grid x:Name="layoutRoot">

    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <SomeUserControl Grid.Row="0" />

    <ui:AdditionalView.UseCase1>
        <ContentControl>
           <TextBlock>aaa</TextBlock>
         </ContentControl>
     </ui:AdditionalView.UseCase1>

</Grid>

首先,也是最重要的,我需要在 Something.UseCase1 表单中有块。这就是我最终使用附加属性的方式。我定义了 AdditionalView 类并在其上定义了一个名为 UseCase1 的附加属性。

然而,这并没有呈现

<TextBlock>aaa</TextBlock> 

在运行时。

我怎样才能做到这一点?


后来编辑(1) - 我设法让一些东西像这样工作:

<ContentControl Grid.Row="1" Content="{Binding ElementName=layoutRoot, Path=(ui:AdditionalView.UseCase1)}" />

..但它看起来很讨厌。有什么体面的方法可以让它工作吗?

AdditionalView 类:

public class AdditionalView
{
    public static readonly DependencyProperty UseCase1Property = DependencyProperty.RegisterAttached(
        "UseCase1",
        typeof(object),
        typeof(AdditionalView),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender)
    );

    public static void SetUseCase1(UIElement element, Boolean value)
    {
        element.SetValue(UseCase1Property, value);
    }
    public static object GetUseCase1(UIElement element)
    {
        return element.GetValue(UseCase1Property);
    }
}
4

0 回答 0