added samples
[windows-sources.git] / sdk / samples / WPFSamples / BrushesIntroduction / csharp / interactivelineargradientbrushexample.xaml.cs
blob2ea1675245c031f6fb5dfe15e698958ce3c585b6
1 using System;
2 using System.Windows;
3 using System.Windows.Data;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Input;
7 using System.Windows.Navigation;
8 using System.Windows.Shapes;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
12 namespace BrushesIntroduction
15 /// <summary>
16 /// Enables the user to configure a LinearGradientBrush interactively.
17 /// </summary>
18 public partial class InteractiveLinearGradientBrushExample : Page
21 public InteractiveLinearGradientBrushExample()
23 InitializeComponent();
26 private void onPageLoaded(object sender, RoutedEventArgs s)
29 MappingModeComboBox.SelectionChanged += new SelectionChangedEventHandler(mappingModeChanged);
30 onStartPointTextBoxKeyUp(StartPointTextBox, null);
31 onEndPointTextBoxKeyUp(EndPointTextBox, null);
35 // Update the StartPoint and EndPoint markers when the gradient display
36 // element's size changes.
37 private void gradientDisplaySizeChanged(object sender, SizeChangedEventArgs e)
39 // The marker positions only need recalcutated if the brush's MappingMode
40 // is RelativeToBoundingBox.
41 if (InteractiveLinearGradientBrush.MappingMode ==
42 BrushMappingMode.RelativeToBoundingBox)
44 StartPointMarkerTranslateTransform.X =
45 InteractiveLinearGradientBrush.StartPoint.X * e.NewSize.Width;
46 StartPointMarkerTranslateTransform.Y =
47 InteractiveLinearGradientBrush.StartPoint.Y * e.NewSize.Height;
49 EndPointMarkerTranslateTransform.X =
50 InteractiveLinearGradientBrush.EndPoint.X * e.NewSize.Width;
51 EndPointMarkerTranslateTransform.Y =
52 InteractiveLinearGradientBrush.EndPoint.Y * e.NewSize.Height;
56 private void onStartPointTextBoxKeyUp(object sender, KeyEventArgs args)
58 TextBox t = (TextBox)sender;
59 try
61 Point p = Point.Parse(t.Text);
62 if (InteractiveLinearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)
64 StartPointMarkerTranslateTransform.X = p.X * GradientDisplayElement.ActualWidth;
65 StartPointMarkerTranslateTransform.Y = p.Y * GradientDisplayElement.ActualHeight;
67 else
69 StartPointMarkerTranslateTransform.X = p.X;
70 StartPointMarkerTranslateTransform.Y = p.Y;
74 catch (InvalidOperationException ex)
76 // Ignore errors.
78 catch (FormatException formatEx)
80 // Ignore errors.
85 private void onEndPointTextBoxKeyUp(object sender, KeyEventArgs args)
87 TextBox t = (TextBox)sender;
88 try
90 Point p = Point.Parse(t.Text);
91 if (InteractiveLinearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)
93 EndPointMarkerTranslateTransform.X = p.X * GradientDisplayElement.ActualWidth;
94 EndPointMarkerTranslateTransform.Y = p.Y * GradientDisplayElement.ActualHeight;
96 else
98 EndPointMarkerTranslateTransform.X = p.X;
99 EndPointMarkerTranslateTransform.Y = p.Y;
104 catch (InvalidOperationException ex)
106 // Ignore errors.
108 catch (FormatException formatEx)
110 // Ignore errors.
115 // Determine whether the user clicked a marker.
116 private void gradientDisplayMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
118 if (e.OriginalSource is Shape)
120 SetValue(SelectedMarkerProperty, (Shape)e.OriginalSource);
122 else
123 SetValue(SelectedMarkerProperty, null);
126 // Determines whether the user just finished dragging a marker. If so,
127 // this method updates the brush's StartPoint or EndPoint property,
128 // depending on which marker was dragged.
129 private void gradientDisplayMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
131 Point clickPoint = e.GetPosition(GradientDisplayElement);
132 Shape s = (Shape)GetValue(SelectedMarkerProperty);
133 if (s == EndPointMarker || s == StartPointMarker)
135 TranslateTransform translation = (TranslateTransform)s.RenderTransform;
136 translation.X = clickPoint.X;
137 translation.Y = clickPoint.Y;
138 SetValue(SelectedMarkerProperty, null);
139 Mouse.Synchronize();
141 Point p;
142 if (InteractiveLinearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)
144 p = new Point(clickPoint.X / GradientDisplayElement.ActualWidth,
145 clickPoint.Y / GradientDisplayElement.ActualHeight);
147 else
150 p = clickPoint;
154 if (s == StartPointMarker)
157 InteractiveLinearGradientBrush.StartPoint = p;
158 StartPointTextBox.Text = p.X.ToString("F4") + "," + p.Y.ToString("F4");
160 else
162 InteractiveLinearGradientBrush.EndPoint = p;
163 EndPointTextBox.Text = p.X.ToString("F4") + "," + p.Y.ToString("F4");
168 // Update the StartPoint or EndPoint when the user drags one of the
169 // points with the mouse.
170 private void gradientDisplayMouseMove(object sender, MouseEventArgs e)
172 Point currentPoint = e.GetPosition(GradientDisplayElement);
173 Shape s = (Shape)GetValue(SelectedMarkerProperty);
175 // Determine whether the user dragged a StartPoint or
176 // EndPoint marker.
177 if (s == EndPointMarker || s == StartPointMarker)
180 // Move the selected marker to the current mouse position.
181 TranslateTransform translation = (TranslateTransform)s.RenderTransform;
182 translation.X = currentPoint.X;
183 translation.Y = currentPoint.Y;
184 Mouse.Synchronize();
186 Point p;
188 // Calculate the StartPoint or EndPoint.
189 if (InteractiveLinearGradientBrush.MappingMode ==
190 BrushMappingMode.RelativeToBoundingBox)
192 // If the MappingMode is relative, compute the relative
193 // value of the new point.
194 p = new Point(currentPoint.X / GradientDisplayElement.ActualWidth,
195 currentPoint.Y / GradientDisplayElement.ActualHeight);
197 else
199 // If the MappingMode is absolute, there's no more
200 // work to do.
201 p = currentPoint;
205 if (s == StartPointMarker)
207 // If the selected marker is the StartPoint marker,
208 // update the brush's StartPoint.
209 InteractiveLinearGradientBrush.StartPoint = p;
210 StartPointTextBox.Text = p.X.ToString("F4") + "," + p.Y.ToString("F4");
212 else
214 // Otherwise, update the brush's EndPoint.
215 InteractiveLinearGradientBrush.EndPoint = p;
216 EndPointTextBox.Text = p.X.ToString("F4") + "," + p.Y.ToString("F4");
224 // Updates the StartPoint and EndPoint and their markers when
225 // the user changes the brush's MappingMode.
226 private void mappingModeChanged(object sender, SelectionChangedEventArgs e)
229 Point oldStartPoint = InteractiveLinearGradientBrush.StartPoint;
230 Point newStartPoint = new Point();
231 Point oldEndPoint = InteractiveLinearGradientBrush.EndPoint;
232 Point newEndPoint = new Point();
235 if (InteractiveLinearGradientBrush.MappingMode ==
236 BrushMappingMode.RelativeToBoundingBox)
239 // The MappingMode changed from absolute to relative.
240 // To find the new relative point, divide the old absolute points
241 // by the painted area's width and height.
242 newStartPoint.X = oldStartPoint.X / GradientDisplayElement.ActualWidth;
243 newStartPoint.Y = oldStartPoint.Y / GradientDisplayElement.ActualHeight;
244 InteractiveLinearGradientBrush.StartPoint = newStartPoint;
246 newEndPoint.X = oldEndPoint.X / GradientDisplayElement.ActualWidth;
247 newEndPoint.Y = oldEndPoint.Y / GradientDisplayElement.ActualHeight;
248 InteractiveLinearGradientBrush.EndPoint = newEndPoint;
251 else
253 // The MappingMode changed from relative to absolute.
254 // To find the new absolute point, multiply the old relative points
255 // by the painted area's width and height.
256 newStartPoint.X = oldStartPoint.X * GradientDisplayElement.ActualWidth;
257 newStartPoint.Y = oldStartPoint.Y * GradientDisplayElement.ActualHeight;
258 InteractiveLinearGradientBrush.StartPoint = newStartPoint;
260 newEndPoint.X = oldEndPoint.X * GradientDisplayElement.ActualWidth;
261 newEndPoint.Y = oldEndPoint.Y * GradientDisplayElement.ActualHeight;
263 InteractiveLinearGradientBrush.EndPoint = newEndPoint;
267 // Update the StartPoint and EndPoint display text.
268 StartPointTextBox.Text = newStartPoint.X.ToString("F4") +
269 "," + newStartPoint.Y.ToString("F4");
270 EndPointTextBox.Text = newEndPoint.X.ToString("F4") +
271 "," + newEndPoint.Y.ToString("F4");
274 // Update the markup display whenever the brush changes.
275 private void onInteractiveLinearGradientBrushChanged(object sender, EventArgs e)
277 if (GradientDisplayElement != null)
279 markupOutputTextBlock.Text =
280 generateLinearGradientBrushMarkup(InteractiveLinearGradientBrush);
284 // Helper method that displays the markup of interest for
285 // creating the specified brush.
286 private static string generateLinearGradientBrushMarkup(LinearGradientBrush theBrush)
288 System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();
289 sBuilder.Append("<" + theBrush.GetType().Name + "\n" +
290 " StartPoint=\"" + theBrush.StartPoint.ToString() + "\"" +
291 " EndPoint=\"" + theBrush.EndPoint.ToString() + "\" \n" +
292 " MappingMode=\"" + theBrush.MappingMode.ToString() + "\"" +
293 " SpreadMethod=\"" + theBrush.SpreadMethod.ToString() + "\"\n" +
294 " ColorInterpolationMode=\"" + theBrush.ColorInterpolationMode.ToString() + "\"" +
295 " Opacity=\"" + theBrush.Opacity.ToString() + "\"" + ">\n");
297 foreach (GradientStop stop in theBrush.GradientStops)
299 sBuilder.Append
301 " <GradientStop Offset=\"" + stop.Offset.ToString("F4")
302 + "\" Color=\"" + stop.Color.ToString() + "\" />\n"
306 sBuilder.Append("</LinearGradientBrush>");
307 return sBuilder.ToString();
310 public static readonly DependencyProperty SelectedMarkerProperty =
311 DependencyProperty.Register
312 ("SelectedMarker", typeof(Shape), typeof(InteractiveLinearGradientBrushExample),
313 new PropertyMetadata(null));
318 [ValueConversion(typeof(object), typeof(string[]))]
319 public class EnumPossibleValuesToStringArrayConverter : IValueConverter
321 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
325 return new System.Collections.ArrayList(Enum.GetNames(value.GetType()));
329 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
331 return null;
335 [ValueConversion(typeof(Point), typeof(string))]
336 public class PointToStringConverter : IValueConverter
338 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
341 Point p = (Point)value;
342 return p.X.ToString("F4") + "," + p.Y.ToString("F4");
346 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
350 return Point.Parse((string)value);
352 catch (System.InvalidOperationException ex)
354 return null;
359 [ValueConversion(typeof(double), typeof(string))]
360 public class DoubleToStringConverter : IValueConverter
362 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
365 double d = (double)value;
366 return d.ToString("F4");
370 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
374 return Double.Parse((string)value);
376 catch (System.InvalidOperationException ex)
378 return null;