2013-10-23 71 views
0

我有用於將圖像定義XAML如下:對於變焦應用縮放,也翻轉圖像wpf?

<ControlTemplate> 
       <Grid>     
        <Image x:Name="documentPage" Source="{Binding ImageSource}" 
          VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Stretch="Fill">  
         <Image.LayoutTransform> 
          <TransformGroup> 
           <ScaleTransform ScaleX="{Binding ScaleFactor}" ScaleY="{Binding ScaleFactor}"/> 
          </TransformGroup> 
         </Image.LayoutTransform> 
        </Image>      
       </Grid> 
      </ControlTemplate> 

和上的按鈕或縮小我遞增0.1比例因子(放大)或遞減0.1(縮小)。

現在我也想申請翻轉圖像,以及...像垂直或水平翻轉....我該怎麼辦呢? 謝謝!

+0

你總是可以否定你的佈局規模的x/y屬性變換 – Andy

回答

4

,你已經應用到LayoutTransform你可以把儘可能多的大規模改造,只要你想,你可以結合另一規模改造,以財產的TransformGroup內。

<Image.LayoutTransform> 
    <TransformGroup> 
     <ScaleTransform ScaleX="{Binding ScaleFactor}" ScaleY="{Binding ScaleFactor}"/> 
     <ScaleTransform ScaleX="-1" ScaleY="1"/> 
    </TransformGroup> 
</Image.LayoutTransform> 

,而不是-1和1在第二變換它們綁定到一個屬性在您的視圖模型(顯然是一個轉換器將如果你的flipx和flipy性質是布爾需要)

在這裏,我已經創建一個簡單的例子,顯示您的問題中使用轉換器將布爾屬性轉換爲比例尺的所有功能,以轉換ScaleX和ScaleY。

enter image description here

XAML

<Window x:Class="flipx.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:flipx" 
      DataContext="{Binding RelativeSource={RelativeSource Self}}" 
      Title="MainWindow" Height="350" Width="525"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*"/> 
       <RowDefinition Height="30"/> 
      </Grid.RowDefinitions> 
      <Ellipse Grid.Row="0" Width="100" Height="100"> 
       <Ellipse.Fill> 
        <LinearGradientBrush > 
         <GradientStop Color="Red"/> 
         <GradientStop Color="#FF2300FF" Offset="1"/> 
        </LinearGradientBrush> 
       </Ellipse.Fill> 
       <Ellipse.LayoutTransform> 
        <TransformGroup> 
         <ScaleTransform ScaleX="{Binding ScaleFactor}" ScaleY="{Binding ScaleFactor}"/> 
         <ScaleTransform ScaleX="{Binding FlipX, Converter={local:BooleanToScaleConverter}}" ScaleY="{Binding FlipY, Converter={local:BooleanToScaleConverter}}"/> 
        </TransformGroup> 
       </Ellipse.LayoutTransform> 
      </Ellipse> 

      <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center"> 
       <CheckBox Margin="5" IsChecked="{Binding FlipX}">FlipX</CheckBox> 
       <CheckBox Margin="5" IsChecked="{Binding FlipY}">FlipY</CheckBox>   
       <Slider Minimum="0.001" Maximum="5" Value="{Binding ScaleFactor}" Width="150"/> 
      </StackPanel> 
     </Grid> 

    </Window> 

C#

using System; 
using System.Windows; 
using System.Windows.Data; 
using System.Windows.Markup; 

namespace flipx 
{ 
    public class BooleanToScaleConverter : MarkupExtension, IValueConverter 
    { 
     static BooleanToScaleConverter converter; 

     public BooleanToScaleConverter() { } 

     public override object ProvideValue(IServiceProvider serviceProvider) 
     { 
      if (converter == null) converter = new BooleanToScaleConverter(); 
      return converter; 
     } 

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      bool boolValue = (bool)value; 
      return boolValue ? -1 : 1; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public partial class MainWindow : Window 
    { 
     public double ScaleFactor 
     { 
      get { return (double)GetValue(ScaleFactorProperty); } 
      set { SetValue(ScaleFactorProperty, value); } 
     } 
     public static readonly DependencyProperty ScaleFactorProperty = 
      DependencyProperty.Register("ScaleFactor", typeof(double), typeof(MainWindow), new PropertyMetadata(1d)); 

     public bool FlipX 
     { 
      get { return (bool)GetValue(FlipXProperty); } 
      set { SetValue(FlipXProperty, value); } 
     } 
     public static readonly DependencyProperty FlipXProperty = 
      DependencyProperty.Register("FlipX", typeof(bool), typeof(MainWindow), new PropertyMetadata(false)); 

     public bool FlipY 
     { 
      get { return (bool)GetValue(FlipYProperty); } 
      set { SetValue(FlipYProperty, value); } 
     }   
     public static readonly DependencyProperty FlipYProperty = 
      DependencyProperty.Register("FlipY", typeof(bool), typeof(MainWindow), new PropertyMetadata(false)); 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 
} 
+0

感謝安迪......你能詳細一點。 ..我是新來的wpf ...我如何使用轉換器?再次感謝... – user1202434

+0

你的觀點結合的性質在不具有完全匹配類型的視圖模型的屬性時,使用轉換器(或你想要做一些其他操作),這也正是它聽起來像它它只是將視圖模型屬性轉換爲視圖的其他內容。 – Andy

+0

我希望我能多勞多得。感謝百萬幫助初學者。再次感謝 – user1202434