WPF 清除 UserControl 的子项 [英] WPF Clear UserControl's children

查看:29
本文介绍了WPF 清除 UserControl 的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 中创建了一个 UserControl,并且 UserControl 可以工作,因此当用户将鼠标移到控件上时,它的子控件应该被删除,但我似乎没有找到 Children 属性或类似的东西..

I'm creating a UserControl in WPF, and the UserControl works so that when the user moves mouse over the control, it's childcontrols should be removed, but I don't seem to find the Children property or anything like that..

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 消失:

And in code I need to do something like this to get the TextBlock go away:

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

推荐答案

UserControl 的子元素"包含在 Content 属性.您可以将其设置为 null 以删除内容.

The "child element" of the UserControl is contained in the Content property. You can set it to null in order to remove the content.

private void UserControl_MouseEnter_1(object sender, MouseEventArgs e)
{
    Content = null;
}

这篇关于WPF 清除 UserControl 的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆