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>
相关推荐
mit6.82412 分钟前
Linux下C#项目构建
开发语言·c#
群联云防护小杜17 分钟前
从一次 DDoS 的“死亡回放”看现代攻击链的进化
开发语言·python·linq
霸敛22 分钟前
好家园房产中介网后台管理完整(python+flask+mysql)
开发语言·python·flask
Momentary_SixthSense41 分钟前
RESP协议
java·开发语言·javascript·redis·后端·python·mysql
only-lucky2 小时前
C++中的 Eigen库使用
开发语言·c++
bianshaopeng2 小时前
ubuntu go 环境变量配置
开发语言·ubuntu·golang
元清加油2 小时前
【Goland】:协程和通道
服务器·开发语言·后端·网络协议·golang
lindexi2 小时前
WPF 引用 ASP.NET Core 的 AOT 版本
wpf·asp.netcore
广州智造2 小时前
EPLAN教程:流体工程
开发语言·人工智能·python·算法·软件工程·软件构建
wjm0410062 小时前
ios八股文 -- Objective-c
开发语言·ios·objective-c