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
- Anonymous
January 03, 2008
PingBack from http://geeklectures.info/2008/01/03/%ef%bd%97%ef%bd%90%ef%bd%86%ef%bc%93%ef%bc%8e%ef%bc%95%e3%81%ae%e6%96%b0%e6%a9%9f%e8%83%bd%e2%91%a4-viewport2dvisual3d/