WPF Clear UserControl的孩子们

时间:2013-02-20 21:04:29

标签: c# wpf

我正在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!!!
}

1 个答案:

答案 0 :(得分:3)

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

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