Не знаю подходит ли решение, но вот:
| Код | using System; using System.Windows; using System.Windows.Media;
namespace DrawingVisualSample { public class MyVisual : FrameworkElement { private DrawingVisual drawingVisual;
public MyVisual() { this.Loaded += new RoutedEventHandler( OnLoaded ); }
void OnLoaded( object sender, RoutedEventArgs e ) { drawingVisual = CreateVisual();
// Call AddVisualChild and AddLogicalChild so the element hosting these visuals // knows about them and are accounted for in the layout process. This is what // Gets custom visuals actually rendered. AddVisualChild( drawingVisual ); AddLogicalChild( drawingVisual ); }
/// <summary> /// Notice how the Create functions aren't being called in OnRender. /// </summary> private DrawingVisual CreateVisual() { DrawingVisual mVisual = new DrawingVisual(); using (DrawingContext drawingContext = mVisual.RenderOpen()) { for (int i = 0; i < 16; i++) for (int j = 0; j < 16; j++) { Rect rect = new Rect(i * 32, j * 32, 32, 32); Brush brush = (i + j)%2 == 0 ? Brushes.DimGray : Brushes.DeepSkyBlue; drawingContext.DrawRectangle(brush, null, rect); } } return mVisual; }
#region Necessary Overrides -- Needed by WPF to maintain bookkeeping of our hosted visuals protected override int VisualChildrenCount { get { return 1; } }
protected override Visual GetVisualChild( int index ) { if ( index < 0 || index >= 1 ) throw new IndexOutOfRangeException( "index" ); return drawingVisual; } #endregion } } |
| Код | <Window x:Class="DrawingVisualSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DrawingVisualSample" Title="DrawingVisualSample" Width="800" Height="600"> <Grid> <local:MyVisual Width="520" Height="520"> <local:MyVisual.RenderTransform> <TranslateTransform X="0" Y="0"/> </local:MyVisual.RenderTransform> <local:MyVisual.Triggers> <EventTrigger RoutedEvent="FrameworkElement.MouseEnter"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard TargetProperty="(local:MyVisual.RenderTransform).(TranslateTransform.X)"> <DoubleAnimation Duration="0:0:2" To="200"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="FrameworkElement.MouseLeave"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard TargetProperty="(local:MyVisual.RenderTransform).(TranslateTransform.X)"> <DoubleAnimation Duration="0:0:2"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </local:MyVisual.Triggers> </local:MyVisual> </Grid> </Window> |
Исходники взял http://jerryclin.wordpress.com/2007/12/19/lightweight-drawing-with-drawingvisuals-part-2-of-2/ и немного переделал: |