2009-12-11 2 views
0

윈도우가로드되는 동안 그리드가 포함 된 주 창이 있습니다. 사용자 컨트롤의 인스턴스를 동적으로 만들고 그리드에 추가합니다. 메인 윈도우의 크기가 변경되었을 때 사용자 컨트롤을 적용하기 위해 사용자 컨트롤의 너비와 높이를 그리드의 ActualWidthActualHeight에 바인딩하고 싶습니다.xaml에서 선언 된 바인딩이 작동하지 않습니다.

첫 번째 방법은 예상대로

Binding widthBinding = new Binding("ActualWidth"); 
widthBinding.Source = panel; 
BindingOperations.SetBinding(uc, WidthProperty, widthBinding); 

Binding heightBinding = new Binding("ActualHeight"); 
heightBinding.Source = panel; 
BindingOperations.SetBinding(uc, HeightProperty, heightBinding); 

panel.Children.Add(uc); 

그것은 일의 window_loaded 이벤트 코드, 같은 장소에서 바인딩 개체를 만드는 것입니다.

두 번째 방법은, 사용자 컨트롤의 XAML 파일에 바인딩 XAML을 사용하는

<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded" 
    Width="{Binding ElementName=ContainerElement, Path=ActualWidth}" 
    Height="{Binding ElementName=ContainerElement, Path=ActualHeight}"> 

또는

<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded" 
     Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualWidth}" 
     Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualHeight}"> 

입니다하지만이 작동하지 않았다.

xaml 접근 방식의 문제점을 알고 있습니까?

+0

게시 한 두 XAML 시나리오에 대한 출력 창 바인딩 오류를 게시 할 수 있습니까? 또한이 UserControl 추가하는 눈금에 대한 XAML 게시 할 수 있습니다. 그리드가이 UserControl의 바로 위 부모입니까? –

+0

새로운 테스트를 만들었습니다. 이제 모든 것이 작동합니다. 사용자 정의 컨트롤의 부모로 모눈을 사용하는 경우 바인딩을 삽입 할 필요가 없습니다. stackpanel을 사용하면 위의 두 xmal 바인딩이 효과적입니다. 내가 이해할 수없는 것은 이전 테스트가 작동하지 않는 이유입니다. 이전 테스트 프로젝트에서 많이 변경 되었기 때문에 이전 코드를 다시 검색 할 수 없었습니다. – intangible02

답변

0

바인딩 대신 정렬을 사용해 볼 수 있습니까?

<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded" 
     HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> 

바인딩의 문제점은 패널에서 뭔가가 증가하게되면 ActualHeightActualWidth이 증가 할 수 있다는 것입니다. 이는 특히 StackPanel 초에서 마찬가지입니다.

Grid을 사용하는 경우 부모 ActualWidthActualHeight에 바인딩 할 수 있습니다. 나는 그것이 때때로 작동한다는 것을 알았지 만, 종종 패널의 어떤 것이 크기를 증가시키고 바인딩을 엉망으로 만든다.

+0

안녕하세요 앤드류, HorizonalAlignment 및 VerticalAlignment가 모두 늘어납니다. – intangible02

관련 문제