다음을 통해 공유


WPF3.5の新機能⑤ Viewport2DVisual3D

これは動作する(ボタンなどの)コントロールが張れる ModelVisual3D 代替要素です。WPF3.0でもModelVisal3D に VisualBrush を使えばコントロールを張ることができました。でもこのコントロールはあくまでも表示要素(Visual)だけで、コントロールとしての機能は使えませんでした。つまりボタンをクリックしても何も起りませんでした。

Viewport2DVisual3D を使えば、3D空間の Viewport2DVisual3Dに張ったコントロールは、2Dと同じように機能します。つまりボタンとして使えます。

次の例は、Viewport2DVisual3D に正方形の GeometryModel3Dを使って3D空間内のTextBoxに入力した文字をデータバインディングでTextBlockに表示させています。また、一番下のボタンのコールバック関数で色を変える処理をしていますが、ちゃんとボタンクリック イベントが処理され、コールバックButton_MouseLeftButtonDownが実行されます。

3D空間で2Dのコントロールを使いたいときには、かなり役に立つ要素です。

<Viewport2DVisual3D x:Name="myModel1“  

   Geometry="{StaticResource mySquare}" >

 <Viewport2DVisual3D.Transform>

  <RotateTransform3D>

   <RotateTransform3D.Rotation>

    <AxisAngleRotation3D Angle="-45" Axis="0 1 0" />

   </RotateTransform3D.Rotation>

  </RotateTransform3D>

 </Viewport2DVisual3D.Transform>

 <Viewport2DVisual3D.Material>

 <DiffuseMaterial  Brush="White"

   Viewport2DVisual3D.IsVisualHostMaterial="True"/>

  </Viewport2DVisual3D.Material>

<!-- ここから 2D 要素 -->

  <StackPanel>

   <TextBox Name="myInput" Background="White"

      Width="100" Height="20"/>

   <TextBlock Name="myText" Background="Red"

      Width="50" Height="50"

     Text="{Binding ElementName=myInput, Path=Text}“ />

   <Button

      PreviewMouseLeftButtonDown="Button_MouseLeftButtonDown">

      Change Color

   </Button>

  </StackPanel>

</Viewport2DVisual3D>

Comments