WPF中的画板InkCanvas

本文提供了一个WPF中InkCanvas组件使用的详细示例,包括如何定义和绑定不同的绘画属性及编辑模式,以及如何实现画布内容的同步显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文参考自http://www.cnblogs.com/zhouyinhui/archive/2007/08/03/841569.html,表示感谢。

下面的第一个例子提供了可以将画的内容保存为文件或者是图片的功能,是Sacha Barber提供的:

Download

第二个例子的xaml文件如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
<Grid>
        
<Canvas>
            
<Canvas.Resources>
                
<!--Define an array containing the InkEditingMode Values.-->
                <!--学习这种提供集合的方法-->
                
<x:Array x:Key="MyEditingModes" x:Type="{x:Type InkCanvasEditingMode}">
                    
<x:Static Member="InkCanvasEditingMode.Ink"/>
                    
<x:Static Member="InkCanvasEditingMode.Select"/>
                    
<x:Static Member="InkCanvasEditingMode.EraseByPoint"/>
                    
<x:Static Member="InkCanvasEditingMode.EraseByStroke"/>
                
</x:Array>
                    
<!--Define an array containing some DrawingAttributes.-->
                
<x:Array x:Key="MyDrawingAttributes" x:Type="{x:Type DrawingAttributes}">
                    
<!--the color,and size-->
                    
<DrawingAttributes Color="Black" FitToCurve="true" 
                         Width
="3" Height="3"/>
                    
<DrawingAttributes Color="Blue" FitToCurve="false" 
                         Width
="5" Height="5"/>
                    
<DrawingAttributes Color="Red" FitToCurve="true" 
                         Width
="7" Height="7"/>
                
</x:Array>

                
<!--Create a DataTemplate to display the DrawingAttributes shown above-->
                
<DataTemplate DataType="{x:Type DrawingAttributes}" >
                    
<Border Width="80" Height="{Binding Path=Height}">
                        
<Border.Background >
                            
<SolidColorBrush Color="{Binding Path=Color}"/>
                        
</Border.Background>
                    
</Border>
                
</DataTemplate>
            
</Canvas.Resources>

            
<!--Bind the first InkCavas' DefaultDrawingAtributes to a 
      Listbox, called lbDrawingAttributes, and its EditingMode to 
      a ListBox called lbEditingMode.
-->
            
<InkCanvas Name="ic" Background="LightGray" 
             Canvas.Top
="0" Canvas.Left="0" 
             Height
="400" Width="200"
             DefaultDrawingAttributes
="{Binding 
                  ElementName=lbDrawingAttributes, Path=SelectedItem}"
             EditingMode=
                  
"{Binding ElementName=lbEditingMode, Path=SelectedItem}"
             
>
            
</InkCanvas>

            
<!--Bind the Strokes, DefaultDrawingAtributes, and, EditingMode properties of
      the second InkCavas the first InkCanvas.
-->
            
<InkCanvas Background="LightBlue"  
             Canvas.Top
="0" Canvas.Left="200" 
             Height
="400" Width="200" 
             Strokes
="{Binding ElementName=ic, Path=Strokes}" 
             DefaultDrawingAttributes
="{Binding 
                  ElementName=ic, Path=DefaultDrawingAttributes}" 
             EditingMode="{Binding ElementName=ic, Path=EditingMode}">

                
<InkCanvas.LayoutTransform>
                    
<ScaleTransform ScaleX="-1" ScaleY="1" />
                
</InkCanvas.LayoutTransform>

            
</InkCanvas>

            
<!--Use the array, MyEditingModes, to populate a ListBox-->
            
<ListBox Name="lbEditingMode" 
           Canvas.Top
="0" Canvas.Left="450" 
           Height
="100" Width="100"
           ItemsSource
="{StaticResource MyEditingModes}" />

            
<!--Use the array, MyDrawingAttributes, to populate a ListBox-->
            
<ListBox Name="lbDrawingAttributes" 
           Canvas.Top
="150" Canvas.Left="450" 
           Height
="100" Width="100"
           ItemsSource
="{StaticResource MyDrawingAttributes}" />

        
</Canvas>
    
</Grid>
可以从下面下载:
Download
posted on 2008-08-22 16:20 cutebear 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/bear831204/archive/2008/08/22/1274200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值