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>
相关推荐
吃喝不愁霸王餐APP开发者18 小时前
基于Spring Cloud Gateway实现对外卖API请求的统一鉴权与流量染色
java·开发语言
源之缘-OFD先行者18 小时前
全栈开发实战:WPF+FFmpeg+GIS,打造工业级雷达探测终端
ffmpeg·wpf
心疼你的一切19 小时前
三菱FX5U PLC与C#通信开发指南
开发语言·单片机·c#
Tim_1019 小时前
【C++入门】04、C++浮点型
开发语言·c++
@淡 定19 小时前
Java内存模型(JMM)详解
java·开发语言
谈笑也风生19 小时前
经典算法题型之复数乘法(二)
开发语言·python·算法
hkNaruto19 小时前
【C++】记录一次C++程序编译缓慢原因分析——滥用stdafx.h公共头文件
开发语言·c++
先知后行。20 小时前
python的类
开发语言·python
派大鑫wink20 小时前
【Day12】String 类详解:不可变性、常用方法与字符串拼接优化
java·开发语言
JIngJaneIL20 小时前
基于springboot + vue健康管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端