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

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

<!-- 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用于绑定

        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 属性来设置外部内容。例如:

<!-- 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>

相关推荐

  1. WPF 预留一个调用使用

    2024-02-20 07:14:03       49 阅读
  2. [C# WPF] 如何添加边框(Border)?

    2024-02-20 07:14:03       54 阅读
  3. C#WPF增加滚动条

    2024-02-20 07:14:03       40 阅读
  4. WPF 使用Image显示图片

    2024-02-20 07:14:03       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-20 07:14:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-20 07:14:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-20 07:14:03       87 阅读
  4. Python语言-面向对象

    2024-02-20 07:14:03       96 阅读

热门阅读

  1. WPF大杂烩

    2024-02-20 07:14:03       46 阅读
  2. OFD文件WEB前端展示-easyofd(1.0.6)

    2024-02-20 07:14:03       49 阅读
  3. 历年CSP-J(NOIP普及组)考点分析与分类汇总

    2024-02-20 07:14:03       46 阅读
  4. ADO.NET事务处理

    2024-02-20 07:14:03       47 阅读
  5. oracle和mysql语句有哪些异同点?

    2024-02-20 07:14:03       62 阅读
  6. 如何在Win11系统中使用ubuntu(WSL)终端编译 Rust 程序

    2024-02-20 07:14:03       52 阅读
  7. Xilinx(AMD) 7系列FPGA配置引脚说明

    2024-02-20 07:14:03       49 阅读
  8. 关于Future的使用

    2024-02-20 07:14:03       42 阅读
  9. 处理目标检测中的类别不均衡问题

    2024-02-20 07:14:03       50 阅读