WPF 清除 UserControl 的子级

2024-03-04

我正在 WPF 中创建一个 UserControl,并且 UserControl 的工作原理是当用户将鼠标移动到该控件上时,它的子控件应该被删除,但我似乎没有找到 Children 属性或类似的东西。

XAML 在这里:

<UserControl x:Class="myTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         Name="thisTextBox"
         mc:Ignorable="d" d:DesignWidth="300" Height="57"     MouseEnter="UserControl_MouseEnter_1" MouseLeave="UserControl_MouseLeave_1">
<TextBlock Name="TypeText" TextWrapping="NoWrap" Text="" />
</UserControl>

在代码中我需要做这样的事情来让 TextBlock 消失:

private void UserControl_MouseEnter_1(object sender, MouseEventArgs e)
{
     Children.Clear(); // There is no such thing as children here!!!
}

UserControl 的“子元素”包含在Content http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content.aspx财产。您可以将其设置为null以删除内容。

private void UserControl_MouseEnter_1(object sender, MouseEventArgs e)
{
    Content = null;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF 清除 UserControl 的子级 的相关文章

随机推荐