1

ActualWidthActualHeight의 값이 0이 아니도록 Window에서 해당 컨트롤을 생성자에서 측정하도록하려면 어떻게해야합니까? 다음은 내 문제를 보여주는 샘플입니다 (저는 Measure 및 Arrange 함수를 호출하려고했지만 어쩌면 잘못된 방식으로 시도했습니다).WPF 종속 속성 변경됨 ActualWidth == 0

XAML : 뒤에

<Window x:Class="WpfApplication7.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     WindowStartupLocation="CenterScreen" 
     Title="WPF Diagram Designer" 
     Background="#303030" 
     Height="600" Width="880" x:Name="Root"> 

    <Grid x:Name="LayoutRoot"> 
     <DockPanel> 
      <TextBox DockPanel.Dock="Top" Text="{Binding ElementName=Root, Mode=TwoWay, Path=Count}"/> 
      <Button DockPanel.Dock="Top" Content="XXX"/> 
      <Canvas x:Name="MainCanvas"> 

      </Canvas> 
     </DockPanel> 
    </Grid> 
</Window> 

번호 :이 예

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Shapes; 
using System; 
using System.Windows.Media; 

namespace WpfApplication7 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
      Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); 
      Arrange(new Rect(DesiredSize)); 
      Count = 6; 
     } 

     public static readonly DependencyProperty CountProperty = DependencyProperty.Register("Count", 
      typeof(int), typeof(Window1), new FrameworkPropertyMetadata(5, CountChanged, CoerceCount)); 

     private static object CoerceCount(DependencyObject d, object baseValue) 
     { 
      if ((int)baseValue < 2) baseValue = 2; 
      return baseValue; 
     } 

     public int Count 
     { 
      get { return (int)GetValue(CountProperty); } 
      set { SetValue(CountProperty, value); } 
     } 

     private static void CountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      Window1 w = d as Window1; 
      if (w == null) return; 
      Canvas c = w.MainCanvas; 
      if (c == null || c.Children == null) return; 
      c.Children.Clear(); 
      if (c.ActualWidth == 0) MessageBox.Show("XXX"); 
      for (int i = 0; i < w.Count; i++) 
       c.Children.Add(new Line() 
       { 
        X1 = c.ActualWidth * i/(w.Count - 1), 
        X2 = c.ActualWidth * i/(w.Count - 1), 
        Y1 = 0, 
        Y2 = c.ActualHeight, 
        Stroke = Brushes.Red, 
        StrokeThickness = 2.0 
       }); 
     } 
    } 
} 

포인트가 오른쪽 가장자리에 왼쪽 가장자리에서 수직 라인의 카운트 번호 그려진 것으로. TextBox의 값을 변경하면 잘 작동하지만 이미 선을 처음부터 그려야합니다.

그래서 코드를 업데이트하여 처음부터 줄을 그려야합니까? 또는 언급 된 코드와 다른 접근 방식이이 목표를 달성하는 데 더 적합할까요?

아무쪼록 고맙습니다.

+1

이 아마도 당신이'보호 재정의 무효 OnContentRendered (EventArgs입니다 전자)'에서 논리를 넣을 수 있습니다, 윈도우의 내용이해야 모두가 그때까지 밖으로 누워와 ActualWith 등 준비를해야합니다. –

+0

그건 내 질문에 대한 답변, 많이 고마워. 답변을 작성해주세요. 나는이 질문을 답글로 표시 할 수 있습니다. –

답변

1

아마도 로직을 OnContentRendered에 넣을 수 있습니다.

Windows Content을 모두 레이아웃해야하며 ActualWidth 등을 준비해야합니다.

예 :

protected override void OnContentRendered(EventArgs e) 
{ 
    base.OnContentRendered(e); 

    // Logic 
} 
관련 문제