2011-05-16 3 views
7

몇 가지 사용자 지정 보안 설정을 기반으로 창 하위 컨트롤을 읽기 전용 및 사용 안 함으로 변경합니다. 이를 수행하기 위해 윈도우가로드 될 때 하위 컨트롤을 반복합니다.WPF - 새로운 시각적 자식 요소가 추가 될 때를 감지하는 방법은 무엇입니까?

잘 작동합니다. 완벽한 99 %.

내 창에는 ComboBox를 기반으로하는 ItemsControl이 있습니다. ComboBox를 변경하면 ItemsControl의 자식 컨트롤이 다시 데이터 바인딩됩니다. 그러나 보안 (읽기 전용/비활성화)은 더 이상 사실이 아닙니다.

솔루션으로 이동하기 전에 ComboBox changed 이벤트를 처리 할 수 ​​있다는 것을 알고 있습니다. 그러나 많은 개발자들이 윈도우 레벨에서 적용 할 수있는 일반적인 솔루션 (생각 : 기본)은 개발자가 윈도우/폼에 추가하는 것과 관계가 없습니다.

내 질문에 (긴 리드가 유감입니다.) 데이터 바인딩과 같은 일부 동적 활동으로 인해 새 자식이 창에 추가되면 어떻게 감지 할 수 있습니까? NewChildAdded 이벤트가 있습니까? DataBindingJustChangedThings 이벤트가 있습니까?

뭔가 있어야합니다.

솔루션에 타이머가 포함 된 경우 회신 할 필요가 없습니다. 내 양식이 너무 복잡하여 추가로드를 처리 할 수 ​​없으며 틱 간의 지연 시간이 보안 문제와 관련이 있습니다.

외부 컨테이너를 읽기 전용으로 설정하거나 사용하지 않도록 설정할 수도 있습니다. 그러나 이것은 익스팬더 (expander), 멀티 라인 텍스트 박스 (multi-line textboxes) 및리스트 박스 (listbox)와 같은 것에 부정적인 영향을 미친다. 이러한 접근 방식은 충분히 입자가 많지 않습니다. 물론, 그것은 우리가 반복을 시작한 곳입니다.

솔루션에 스타일이 포함되어있는 경우 컨트롤별로 컨트롤을 재정의 할 수있는 방법을 포함해야합니다. 확인란과 같은 일부 컨트롤은 UI 레이아웃 용도로 사용할 수 없으므로 비활성화 할 수 없습니다.

제약 조건으로 인해 불편을 끼쳐 드려 죄송합니다. 프로덕션에서 솔루션을 사용할 계획입니다.

감사합니다.

답변

18

OnVisualChildrenChanged을 사용해 보셨습니까?

+0

을 OnVisualChildrenChanged' Canvas '에 공개적으로 액세스 할 수있는 이벤트는 아닙니다. :(상속 할 시간. – IAbstract

+2

캔버스 예. http://stackoverflow.com/questions/5134080/canvas-in-wpf-how-do-i-detect-when-an-element-has-been-added-removed -로부터 –

4

매우 해키하지만 당신은 방법을 OnVisualChildrenChanged 오버라이드 (override) 할 수 있도록 컨트롤에서 상속하지 않는 경우에, 저를 위해 일했다.

LayoutUpdated 이벤트를 청취 할 수 있습니다. 뒤에

<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> 

코드 : 예 울부 짖는 소리에

, 내 그리드라는 GridYouWantToListenTo는 하나 개 또는 두 개의 요소를 추가 처음으로 듣고 싶은 불행하게도,`

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 
      } 
     } 
    } 
} 
관련 문제