//http://simpledbbrowser.codeplex.com/ //License: Microsoft Public License (Ms-PL) using System.Diagnostics; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; namespace AWS.Framework.WPF.Utility { public sealed class Helpers { public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject { for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild<childItem>(child); if (childOfChild != null) return childOfChild; } } return null; } } }
Windows Presentation Foundation
Update Focused Field for FrameworkElement
//http://simpledbbrowser.codeplex.com/ //License: Microsoft Public License (Ms-PL) using System.Diagnostics; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; namespace AWS.Framework.WPF.Utility { public sealed class Helpers { #region Constructor private Helpers() { } #endregion // Constructor #region Methods public static void UpdateFocusedField() { FrameworkElement fwe = Keyboard.FocusedElement as FrameworkElement; if (fwe != null) { BindingExpression expression = null; if (fwe is TextBox) expression = fwe.GetBindingExpression(TextBox.TextProperty); // TODO - developers - add more control types here. Won't be that many. // this would include custom TextBox controls or 3rd party TextBox controls if (expression != null) expression.UpdateSource(); } } #endregion // Methods } }
Change Fill
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006" mc:Ignorable="d" x:Class="PaintDrawExamples.ChangeFill" Width="640" Height="480"> <StackPanel.Resources> <Storyboard x:Key="OnLoaded"/> </StackPanel.Resources> <StackPanel.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/> </EventTrigger> </StackPanel.Triggers> <Path d:LastTangent="0,0" Width="300" Height="82" x:Name="myShape" RenderTransformOrigin="0.5,0.5" Stretch="Fill" Data="M268,209 L134,209 147,249 250,290 z"> <Path.RenderTransform> <TransformGroup> <TranslateTransform X="0" Y="0"/> <ScaleTransform ScaleX="1" ScaleY="1"/> <SkewTransform AngleX="0" AngleY="0"/> <RotateTransform Angle="0"/> <TranslateTransform X="0" Y="0"/> </TransformGroup> </Path.RenderTransform> <Path.Fill> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <GradientStop Color="#11111111" Offset="0"/> <GradientStop Color="#FFFFFFFF" Offset="1"/> </LinearGradientBrush> </Path.Fill> </Path> </StackPanel> //File:Window.xaml.cs using System; using System.IO; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Navigation; namespace PaintDrawExamples { public partial class ChangeFill { public ChangeFill() { this.InitializeComponent(); this.MouseDown += new MouseButtonEventHandler(MouseDownHandler); } private void MouseDownHandler(object sender, MouseButtonEventArgs e) { LinearGradientBrush brush = (LinearGradientBrush)this.myShape.Fill; brush.GradientStops[0].Color = Color.FromRgb(0,0,0); } } }
Change the Visibility property of a UIElement.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Visibility_Layout_Samp.Window1" Title="UIELement.Visibility Sample"> <DockPanel> <StackPanel DockPanel.Dock="Left"> <Button Name="btn1" Height="25" Click="contentVis">Visibility="Visible"</Button> <Button Name="btn2" Height="25" Click="contentHid">Visibility="Hidden"</Button> <Button Name="btn3" Height="25" Click="contentCol">Visibility="Collapsed"</Button> </StackPanel> <StackPanel HorizontalAlignment="Center"> <TextBox Name="tb1" Width="100" Height="50">A TextBox</TextBox> <TextBlock Name="txt1" TextWrapping="Wrap" FontSize="14"/> </StackPanel> </DockPanel> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace Visibility_Layout_Samp { public partial class Window1 : Window { public void contentVis(object sender, RoutedEventArgs e) { tb1.Visibility = System.Windows.Visibility.Visible; txt1.Text = "Visibility is now set to Visible."; } public void contentHid(object sender, RoutedEventArgs e) { tb1.Visibility = System.Windows.Visibility.Hidden; txt1.Text = "Visibility is now set to Hidden."; } public void contentCol(object sender, RoutedEventArgs e) { tb1.Visibility = System.Windows.Visibility.Collapsed; txt1.Text = "Visibility is now set to Collapsed."; } } }
Changing graphical elements
<Window x:Class="ChangeItem.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Change Item"> <Canvas x:Name="mainCanvas"> <Ellipse Canvas.Left="10" Canvas.Top="30" Fill="Indigo" Width="40" Height="20" /> <Ellipse Canvas.Left="20" Canvas.Top="40" Fill="Blue" Width="40" Height="20" /> <Ellipse Canvas.Left="30" Canvas.Top="50" Fill="Cyan" Width="40" Height="20" /> <Ellipse Canvas.Left="40" Canvas.Top="60" Fill="LightGreen" Width="40" Height="20" /> <Ellipse Canvas.Left="50" Canvas.Top="70" Fill="Yellow" Width="40" Height="20" /> </Canvas> </Window> //File:Window.xaml.cs using System.Windows; using System.Windows.Shapes; namespace ChangeItem { public partial class MainWindow : Window { public MainWindow(): base() { InitializeComponent(); mainCanvas.MouseLeftButtonDown += OnClick; } private void OnClick(object sender, RoutedEventArgs e) { Ellipse r = e.Source as Ellipse; if (r != null) { r.Width += 10; } } } }
Change control background in mouse enter and leave
<Window x:Class="Styles.EventSetter" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="EventSetter" Height="300" Width="300"> <Window.Resources> <Style x:Key="MouseOverHighlightStyle"> <Setter Property="TextBlock.Padding" Value="5"/> <EventSetter Event="FrameworkElement.MouseEnter" Handler="element_MouseEnter" /> <EventSetter Event="FrameworkElement.MouseLeave" Handler="element_MouseLeave" /> </Style> </Window.Resources> <StackPanel> <TextBlock Style="{StaticResource MouseOverHighlightStyle}">Hover over me.</TextBlock> <TextBlock Padding="5">asdf</TextBlock> <TextBlock Style="{StaticResource MouseOverHighlightStyle}">Hover over me.</TextBlock> </StackPanel> </Window> //File:Window.xaml.cs using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Styles { public partial class EventSetter : System.Windows.Window { public EventSetter() { InitializeComponent(); } private void element_MouseEnter(object sender, MouseEventArgs e) { ((TextBlock)sender).Background = new SolidColorBrush(Colors.LightGoldenrodYellow); } private void element_MouseLeave(object sender, MouseEventArgs e) { ((TextBlock)sender).Background = null; } } }
Print Visual(Canvas)
<Window x:Class="Printing.PrintVisual" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="PrintVisual" Height="259" Width="282"> <StackPanel> <Canvas Name="canvas"> <TextBlock Canvas.Top="50" Canvas.Left="20" FontSize="25" FontWeight="Bold">Hello There</TextBlock> <Path Fill="Yellow" Stroke="Blue" Margin="5" Canvas.Top="10" Canvas.Left="10" > <Path.Data> <GeometryGroup> <RectangleGeometry Rect="0 0 100 100"></RectangleGeometry> <EllipseGeometry Center="50 50" RadiusX="35" RadiusY="25"></EllipseGeometry> </GeometryGroup> </Path.Data> </Path> </Canvas> <Button Grid.Row="3" Click="cmdPrint_Click">Print</Button> </StackPanel> </Window> //File:Window.xaml.cs using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Printing { public partial class PrintVisual : System.Windows.Window { public PrintVisual() { InitializeComponent(); } private void cmdPrint_Click(object sender, RoutedEventArgs e) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { double zoom = 30; canvas.Background = Brushes.LightSteelBlue; canvas.LayoutTransform = new ScaleTransform(zoom / 100, zoom / 100); Size pageSize = new Size(printDialog.PrintableAreaWidth - 20, printDialog.PrintableAreaHeight - 20); canvas.Measure(pageSize); canvas.Arrange(new Rect(10, 10, pageSize.Width, pageSize.Height)); printDialog.PrintVisual(canvas, "A Scaled Drawing"); canvas.Background = null; canvas.LayoutTransform = null; } } } }