WPF 在控件上预留一个占位给到调用方使用

WPF 在控件上预留一个占位给到调用方使用,首先创建一个自定义控件,并在其中包含一个可用于承载外部内容的容器。

xml 复制代码
<!-- PlaceholderControl.xaml -->
<UserControl x:Class="YourNamespace.PlaceholderControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
         <ContentPresenter Content="{Binding PromptContent, Mode=OneWay, RelativeSource={RelativeSource AncestorType=local:PlaceholderControl}}"/>
    </Grid>
</UserControl>

在后台配置一个依赖属性PromptContent用于绑定

csharp 复制代码
        public static readonly DependencyProperty PromptContentProperty =
            DependencyProperty.Register("PromptContent", typeof(object), typeof(PlaceholderControl));
        /// <summary>
        /// Map 头部显示内容
        /// </summary>
        public object PromptContent
        {
            get { return (object)GetValue(PromptContentProperty); }
            set { SetValue(PromptContentProperty, value); }
        }

使用时,您可以将 PlaceholderControl 放置在您的 WPF 窗口或页面中,并使用 ContentPlaceholder 属性来设置外部内容。例如:

xml 复制代码
<!-- MainWindow.xaml -->
<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YourNamespace"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:PlaceholderControl>
            <local:PlaceholderControl.ContentPlaceholder>
                <!-- 这里放置您想要的外部内容 -->
                <TextBlock Text="这是外部内容"/>
            </local:PlaceholderControl.ContentPlaceholder>
        </local:PlaceholderControl>
    </Grid>
</Window>
相关推荐
牛马也想出海20 分钟前
亚马逊爬虫代理IP:IP类型选择与配置指南
开发语言·php
菜冻鱼36 分钟前
Python-pytorch-高级技巧
开发语言·人工智能·pytorch·python·深度学习·神经网络·聚类
lpfasd12337 分钟前
python webview打包版卡死、开发版正常
开发语言·python
weixin_461408581 小时前
npm npx yarn
开发语言·前端·javascript
菜冻鱼1 小时前
Python-pytorch-模型保存与加载
开发语言·人工智能·pytorch·python·深度学习·机器学习
骇客野人1 小时前
Java SSO 统一认证方案
java·开发语言
李高钢1 小时前
WPF MVVM Light 入门:从零到Hello World
c#·wpf
Code额2 小时前
Python 连接 DeepSeek API,OpenAI
开发语言·python·ai·ai编程
-凌凌漆-2 小时前
【freertos】Task创建(v2)
java·开发语言·算法
牛油果子哥q2 小时前
C++内存模型与深浅拷贝万字详解:栈堆静态内存布局、深浅拷贝底层差异、内存泄漏根治、拷贝崩溃踩坑、手写深拷贝实战
java·开发语言·c++