WPF - 如何检测何时添加新的Visual子元素?

时间:2011-05-16 22:35:18

标签: wpf events visual-tree

基于一些自定义安全设置,我将窗口子控件更改为只读和禁用。为了实现这一点,我在窗口加载时循环访问子控件。

这很好用。 99%完美。

在我的窗口中,我有一个ItemsControl,其内容基于ComboBox。更改ComboBox,ItemsControl中的子控件再次进行数据绑定。但是,安全性(只读/禁用)不再适用。

在你跳转到解决方案之前,我知道我可以处理ComboBox更改的事件;但是,我有很多这样的盒子和wnt通用解决方案可以在窗口级别(想想:基础)应用,无论我的开发人员添加到窗口/窗体。

我的问题(对于长期的引导而言)是,如何通过数据绑定等动态活动将新子项添加到窗口中?是否有NewChildAdded事件?是否有DataBindingJustChangedThings事件?

必须有所作为。

如果您的解决方案包含计时器,则无需回复。我的表单太复杂,无法处理额外的负载 - 并且刻度之间的延迟实际上是一个安全问题。

您可能会想,只需将外部容器设为只读或禁用。但这会对扩展器,多行文本框和列表框等内容产生负面影响。这种方法不够细致。当然,这是我们之前开始迭代的地方。

如果您的解决方案包含样式,则需要包括如何在每个控件的基础上覆盖您的方法。某些控件(如复选框)无法禁用,因为它们在UI布局中有用途。

对不起,但我计划在生产中使用该解决方案。

谢谢。

2 个答案:

答案 0 :(得分:19)

您是否尝试过OnVisualChildrenChanged

    /// <summary>
    /// Handle visual children being added or removed
    /// </summary>
    /// <param name="visualAdded">Visual child added</param>
    /// <param name="visualRemoved">Visual child removed</param>
    protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
    {
        // Track when objects are added and removed
        if (visualAdded != null)
        {
            // Do stuff with the added object
        }
        if (visualRemoved != null)
        {
            // Do stuff with the removed object
        }

        // Call base function
        base.OnVisualChildrenChanged(visualAdded, visualRemoved);
    }

答案 1 :(得分:4)

非常hacky但为我工作,如果你没有从控件继承,所以你不能覆盖 OnVisualChildrenChanged 方法。

您可以收听LayoutUpdated事件。

在下面的示例中,我想第一次收听名为GridYouWantToListenTo的Grid,添加一个或两个元素:

<Window x:Class="WpfApplication23.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication23"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="GridYouWantToListenTo">
    </Grid>
</Window>

代码背后:

using System;
using System.Linq;
using System.Windows;

namespace WpfApplication23
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            GridYouWantToListenTo.LayoutUpdated += GridYouWantToListenTo_LayoutUpdated;
        }

        private int _lastNumbreOfGridChildren = 0;
        private void GridYouWantToListenTo_LayoutUpdated(object sender, EventArgs e)
        {
            var children = GridYouWantToListenTo
                    ?.Children
                    ?.OfType<FrameworkElement>() ?? Enumerable.Empty<FrameworkElement>();

            if (!children.Any())
            {
                _lastNumbreOfGridChildren = 0;
            }

            int currentNumberOfItems = children.Count();

            if (_lastNumbreOfGridChildren == 0 && currentNumberOfItems == 1)
            {
                //Your Logic
            }
            else if (_lastNumbreOfGridChildren == 0 && currentNumberOfItems == 2)
            {
                //Your Logic
            }
        }
    }
}