2010-08-19 6 views
0

ScaleTransform을 사용하여 겪고있는 문제에 당황했습니다. 고정 된 너비와 높이의 그리드가 있습니다. 모눈에는 하나의 자식 인 Rectangle이 포함됩니다. Rectangle의 채우기는 VisualBrush로, 시각적으로 크기가 큰 그리드 외부의 캔바스에 Visual이 바인딩됩니다. 사각형에서 ScaleTransform을 사용하고 ScaleX와 ScaleY를 모두 0.18로 설정합니다. 본질적으로 Rectangle의 시각적 눈금을 내 눈금에 맞추기 위해 크기를 조정하려고합니다. 무슨 일이 벌어지고있는 것인가는 그리드 자체가 축소되어 원하는 것보다 훨씬 작은 결과를 초래한다는 것입니다. 아래 코드를 포함 시켰습니다. 단지 참조 점으로서, 사각형이 바인딩하는 높이와 너비는 본질적으로 900 x 600입니다. 내가 뭘 잘못하고 있는지에 대한 어떤 조언도 크게 감사 할 것입니다.WPF 스케일링 문제

<Grid Height="225" Width="200" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="PART_Content"> 
      <Rectangle Height="{Binding Path=ActualHeight}" Width="{Binding Path=ActualWidth}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
       <Rectangle.Fill> 
        <VisualBrush Visual="{Binding}"/> 
       </Rectangle.Fill> 
       <Rectangle.RenderTransform> 
        <ScaleTransform ScaleX="0.183" ScaleY="0.183"/> 
       </Rectangle.RenderTransform> 
      </Rectangle> 
     </Grid> 

답변

0

Canvas 요소에 대한 XAML을 게시 할 수 있습니까? 는 I 시도를 다음과

<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 

     <Grid ShowGridLines="True" Height="225" Width="200" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="PART_Content"> 
      <Rectangle Height="{Binding Path=ActualHeight}" Width="{Binding Path=ActualWidth}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
       <Rectangle.Fill> 
        <VisualBrush Visual="{Binding ElementName=theCanvas}"/> 
       </Rectangle.Fill> 
       <Rectangle.RenderTransform> 
        <ScaleTransform ScaleX="0.183" ScaleY="0.183"/> 
       </Rectangle.RenderTransform> 
      </Rectangle> 
     </Grid> 

     <Canvas x:Name="theCanvas" Grid.Row="1"> 
      <Rectangle Fill="Brown" Height="300" Width="300" /> 

     </Canvas> 
    </Grid> 
0

이 ActualWidth와의 ActualHeight 무엇 나는 당신이 가고있는 행동 무엇입니까 (사각형의 크기를 조절할를 그리드가 제대로 크기입니다)? WPF에서 보통 의미하는대로 ActualHeight 및 ActualWidth 속성을 오해하지 않는 한 은 DP가 아니기 때문에 에 바인딩 할 수 없습니다. 아래에 지적했듯이 이들은 읽기 전용 의존성 속성입니다. 이것이 CustomControl 스타일에 있다고 가정하면 Binding을 먼저 TemplateBinding으로 변경해야합니다.

바인딩을 제거하고 본질적으로 정상적으로 보이는 XAML의 정적 버전을 만들었습니다. 그리드에 대해 Part_Content가 정의되었으므로이 xaml이 사용자 정의 컨트롤 스타일의 일부인지 궁금합니다. CustomControl의 코드가 PART_Content를 통해 그리드를 조작하고 있습니까?

<Grid Height="225" Width="200" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="PART_Content" Background="Red"> 
<Rectangle Height="225" 
      Width="200" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch" 
      Fill="Blue"> 

      <Rectangle.RenderTransform> 
       <ScaleTransform ScaleX="0.183" ScaleY="0.183"/> 
      </Rectangle.RenderTransform> 
     </Rectangle> 
    </Grid> 
+0

ActualHeight 및 ActualWidth는 읽기 전용이므로 DependencyProperties입니다. Chris가하는 것처럼 Binding Source로 사용할 수있는 이유가 없습니다. - Source는 CLR 속성 일 수 있습니다. DP가되어야하는 대상입니다. 이 경우 읽기 전용이므로 목표로 사용할 수 없으므로 둘 중 하나에 값을 할당 할 수 없습니다. –

+0

"원본은 모든 CLR 속성이 될 수 있습니다."* 그러나 * 바인딩은 INotifyPropertyChanged에 의해 백업되어야 함을 의미합니다. 그것은 내가 DP가 아니라고 착각 할 수 있다고 말했습니다. 솔직히이 코드는 매우 이상하게 보입니다. 실제로 ActualHeight 및 ActualWidth에 바인딩하려는 경우 Binding이 아니라 TemplateBinding을 사용해야합니다. 당신은 datacontext에서 무언가에 바인딩하기를 원할 때 바인딩을 사용하며, 자신의 VM에 ActualHeight/Width가 없다는 것을 확신합니다. – NVM