WPF:管理 ContentPresenter 中所有控件的边距

标签 wpf contentpresenter

是否可以定义 ContentPresenter 的“呈现行为”?以便将填充应用于其内容?

现在我有一个 ContentPresenter并定义所有 UserControl 的边距可以成为此 ContentPresenter 的一部分的 s。

这样做的缺点是它需要重复定义边距和 UserControl s 有点致力于“适合” ContentPresenter .

例如。包含内容展示器的 XAML:

    <ContentPresenter 
        x:Name="SettingsContentPanel" 
        Grid.Row="0" 
        Grid.Column="2" 
        Grid.ColumnSpan="2" 
        Content="{Binding ElementName=SettingsGroupSelector, Path=SelectedItem.Tag}" />

用户控件定义如下:
    <UserControl 
        <!-- left out irrelevant definitions -->
        Margin="5,5,5,5">

最佳答案

您应该能够通过设置 Margin 获得您想要的效果。在您的 ContentPresenter元素本身,例如:

<ContentPresenter 
    x:Name="SettingsContentPanel" 
    Grid.Row="0" 
    Grid.Column="2" 
    Grid.ColumnSpan="2" 
    Content="{Binding ElementName=SettingsGroupSelector, Path=SelectedItem.Tag}" 
    Margin="5,5,5,5" />

另外,如果 Margin各方面都相同,您可以使用简写 Margin="5" .

希望有帮助=D

关于WPF:管理 ContentPresenter 中所有控件的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20777080/

相关文章:

wpf - 尝试创建自定义面板但收到 : The object already has a child and cannot add 'Button' compiler exception

c# - Xamarin 表单 contentpresenter 排错行并在电话上为空

wpf - 我可以有两个 ContentPresenter 指向 ContentTemplate 中的单个 Content 或 ContentSource 吗?

wpf - 是否可以为 WPF 中的静态资源提供类型转换器?

c# - 在 WPF MVVM 中处理模型中的集合

c# - 如何从 C# wpf 应用程序使用 F# 类型

wpf - 更改 WPF 标题栏背景颜色

c# - MediaElement 是否仅在嵌入到 XAML 代码中时才播放?

c# - UWP 继承 self 的用户控件