上一节展示了鼠标拖动改变图片位置和大小,下面介绍下鼠标拖动和控件之间的双向绑定展示,效果如下:
实现步骤:
1、MyImage自定义控件与博文WPF中GDI+图形图像的绘制:(七)绘制图像——鼠标拖动改变位置和大小中一致
2、主窗体xaml
<Window x:Class="DrawDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DrawDemo"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen" ResizeMode="NoResize"
Title="控件绑定" Height="768" Width="1280">
<Grid Background="#f0f0f0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="380"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Canvas x:Name="mainCanvas" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Background="White" Width="960" Height="720" Margin="10,5,0,0"
HorizontalAlignment="Left" VerticalAlignment="Top" ClipToBounds="True"
MouseLeftButtonDown="mainCanvas_MouseLeftButtonDown" MouseLeftButtonUp="mainCanvas_MouseLeftButtonUp" MouseMove="mainCanvas_MouseMove">
</Canvas>
<GroupBox Grid.Row="0" Grid.Column="1" Margin="10" Header="控件绑定">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="选择图片" VerticalAlignment="Center" HorizontalAlignment="Right"></Label>
<Button x:Name="btnChooseImg" Grid.Row="0" Grid.Column="1" Content="选择" VerticalAlignment="Center" HorizontalAlignment="Left" Height="26" Width="80" Margin="10,0,0,0" Click="btnChooseImg_Click"></Button>
<Label Grid.Row="1" Grid.Column="0" Content="X坐标" VerticalAlignment="Center" HorizontalAlignment="Right"></Label>
<wfi:WindowsFormsHost Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Height="26" Width="80" Margin="10,0,0,0">
<winForms:NumericUpDown x:Name="locationX" Minimum="0" Maximum="9999" ValueChanged="LocationX_ValueChanged"></winForms:NumericUpDown>
</wfi:WindowsFormsHost>
<Label Grid.