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

Jer*_*xon 7 wpf events visual-tree

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

这很好用.99%完美.

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

在你跳转到解决方案之前,我知道我可以处理ComboBox更改的事件; 但是,我有很多这样的盒子和Wnt一个通用的解决方案,可以在窗口级应用(认为:基地)无论什么我的开发人员添加到窗口/形式.

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

必须有所作为.

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

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

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

很抱歉有限制,但我打算在生产中使用该解决方案.

谢谢.

Ed *_*tes 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);
    }
Run Code Online (Sandbox Code Playgroud)

  • 是的Canvas:http://stackoverflow.com/questions/5134080/canvas-in-wpf-how-do-i-detect-when-an-element-has-been-added-removed-from-the (2认同)

Eli*_*gan 5

非常hacky,但对我有用,以防您不从控件继承,因此您无法覆盖OnVisualChildrenChanged方法。

您可以收听 LayoutUpdated 事件。

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

<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>
Run Code Online (Sandbox Code Playgroud)

后面的代码:

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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,LayoutUpdated 有一个愚蠢之处,它总是将 null 作为发送者传递,因此您无法消除监听多个控件的歧义,除非您为每个控件使用唯一的处理程序,这有点荒谬。 (2认同)