c# - 如何更改 ContentControl 中的 Content 属性
问题描述
我有自定义 ContentControl
public class MyContentControl: ContentControl
{....}
像这样在 XAML 中定义的内容
<controls:MyContentControl x:Name="myContentControl">
<controls:MyContentControl.Content>
<controls:UserControl1 />
</controls:MyContentControl.Content>
</controls:MyContentControl>
当我启动我的应用程序时,内容会显示在设计器和设备中。但是当我尝试以编程方式更改 Content 属性时,例如
UserControl2 control2 = new UserControl2();
myContentControl.Content = control2;
MyContentControl 什么也不显示。使用标准 ContentControl 给出相同的结果。欢迎任何建议。
解决方案
我按照您的代码制作了简单的代码示例进行测试。这里没有问题。
public class CustomContentControl:ContentControl
{//......}
<Grid>
<local:CustomContentControl x:Name="content">
</local:CustomContentControl>
</Grid>
MyUserControl1 myUserControl1 = new MyUserControl1();
content.Content = myUserControl1;
<UserControl
x:Class="AppContent.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppContent"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<TextBox Text="abc"></TextBox>
</Grid>
您可能已经在代码中进行了一些特定设置。@Martin Zikmund 的建议也是合理的。您可以参考他的建议并检查您的代码。之后,如果您仍然不能解决这个问题,请提供一个Minimal, Complete, and Verifiable example。
推荐阅读
- c# - 创建和下载文件在一个页面上工作而在另一个页面上失败 - ASP.NET
- c++ - 我尝试使用带有 Arduino Nano 的 PN532 板读取 Standard Mifare 卡,但它读取的是我的代码
- javascript - javascript在数组内的对象中添加缺少的键
- groovy - 用于 java/groovy 的 Appium 包装器
- java - 什么时候是钻石算子,什么时候不是?
- python - 在另一个 .txt 文件中逐位搜索一个 .txt 文件的内容段并打印匹配行
- python - 在 Python Pandas 中将 1 分钟 OHLCV 数据重采样为 5 分钟数据
- reactjs - TypeError:无法读取未定义的属性“格式”
- ruby-on-rails - 将 Sinatra 语法转换为 Rails
- python - 特定窗口的键盘输入(python)