יום שלישי, 25 באוקטובר 2011

Snoop - The WPF Spy Utility





What

A random utility made to simplify visual debugging of WPF applications at runtime. It's basically a collection of things that may have seemed useful at one time or another with the common goal of making it easier to track down bugs in WPF applications.
This is not an official tool, just a utility that was found useful and added to when functionality was needed. It's provided in the hopes of helping others.

Usage

Run Snoop.exe. Once it has started it will display a list of all running WPF applications. If your target app is not running yet, just launch it then refresh the list with the Refresh button or by pressing F5. Snoop will automatically find all WPF processes running on the machine to attach to.
Once the application to be snooped is selected, click the Snoop icon ()to launch Snoop.

Troubleshooting

Recently, there was logging ability added to Snoop to see why Snoop does not attach on certain machines. Currently this is not in the release build, so the latest code will need to be downloaded and built for troubleshooting purposes.
If nothing happens when trying to snoop a program, take these troubleshooting steps (Note that logging is not added in the latest release, so you'll have to download the latest code and build it):
1) Go directly to the folder containing the Snoop.exe program being run.
2)  Look for the file SnoopLog.txt in the directory of Snoop.exe (from step 1), and open it. Make sure that the contents of the file are similar to this:
10/09/2011 19:06:02 : Starting the injection process...
10/09/2011 19:06:02 : GetModuleHandleEx successful
10/09/2011 19:06:02 : Got process id
10/09/2011 19:06:02 : Got process handle
10/09/2011 19:06:02 : VirtualAllocEx successful
10/09/2011 19:06:02 : SetWindowsHookEx successful
10/09/2011 19:06:05 : Successfully injected Snoop for process SnoopTestCase.vshost (PID = 7548)
(Make sure that the file ends with "Successfully injected Snoop for process processname (PID = process id)"
If the test above does not pass, contact one of the developers/coordinators (or post to a Discission), and specify what the results in the file are. If the test above passes, it means that Snoop successfully injected itself into the process being "snooped," and the problem lies in the part after the injection process. After Snoop has successfully injected itself into the process, it's running inside the process, and can write to the output window of the debugger debugging the process. At this point, look at the output window of the WPF application being debugged (and attempted to be snooped). The output window should have contents similar to this:
other messages written to output window
Got WM_GOBABYGO message
other messages written to output window
acmLocal = C:\XXX\Snoop.exe$Snoop.SnoopUI$GoBabyGo
other messages written to output window
About to load assembly C:\XXX\Snoop.exe
other messages written to output window
About to load type Snoop.SnoopUI
other messages written to output window
Just loaded the type Snoop.SnoopUI
other messages written to output window
About to invoke GoBabyGo on type Snoop.SnoopUI
other messages written to output window
Return value of GoBabyGo on type Snoop.SnoopUI is True
If you don't see the messages above, try doing this: right before trying to "snoop" your application, try setting Visual Studio to break on all CLR exceptions.
Here are the steps to doing so:
1) Go to the Debug menu in the Visual Studio instance debugging your application.
2) Choose the "Exceptions" menu. An exceptions dialog box should show up.
3) Check "Common Language Runtime Exceptions" under the "Thrown" column.
4) Now try snooping the application. If the SnoopLog.txt contains the contents mentioned above, Visual Studio will break when there's a problem trying to snoop the application. From there, you can use this information to try to troubleshoot (or you can post the information on a Discussions panel, and a developer/coordinator will get back to you).

Building

Snoop.csproj can be built using VS Express, but the complete solution requires the C++ compiler found in full VS. The C++ portion of the solution is provided pre-built and has no WPF dependencies, so you should not have to rebuild it to run.
If any of the ManagedInjector (orManagedInjectorLauncher) projects fail to build, try unloading them from the solution, and rebuild the solution. Snoop detects the environment at runtime, and depending on the environment, Snoop runs the appropriate ManagedInjectorLauncher program at runtime (which is the one that probably built successfully) depending on the environment. For example, if the application that snoop is trying to snoop is built using the 4.0 framework built for the 64 bit process, snoop will run the ManagedInjectorLauncher64-4.0.exe program to inject Snoop into the application, and start "snooping" it. Therefore, if the other ManagedInjector projects fail to build (probably because of an environmental issue), it won't affect snoop at runtime.

Primary View

Graph of the visual tree on the left, list of properties on the selected element in the center, common events & preview area on the right
The selected element will highlight with a red adorner in the target application. Another way of finding elements in the tree is to hold down Ctrl-Shift and mouse over the target application. This will move the selection to the element under the mouse.

Tree View

Navigate the visual tree using this view, F5 will refresh the list of elements. The text on the left is the element name, in brackets is it's type, and on the right is the number of child elements it contains. Number of child elements is quite useful when focusing on performance and trying to keep the element count to a minimum.
If you're tired of searching through debug spew for binding errors, they can be searched for using the drop down beside the filter box.

Property Grid

Lists all the properties on the currently selected element. Writable properties may be edited in the value area. Also displays where the property was picked up from.
Right click will show options to Delve (inspect the value). If the property is a binding then the binding and expression can also be inspected. Useful for debugging those binding errors.

RoutedEvents View

Preview Area

(off by default, for perf)

Zoom View

(accessed from zoom button in Preview Area)
Use mouse wheel to zoom, double-click to reset zoom.  Slider at the top changes the brightness of the background color. +/- keys may also be used to zoom the view.




יום ראשון, 17 באפריל 2011

WPF - Custom Command Binding



XAML:
<UserControl.CommandBindings>
   // Command Binding
      <CommandBinding Command="Save" 
                      CanExecute="SaveCanExecute" 
                      Executed="SaveExecuted">            
      </CommandBinding>
// Custom Command Binding
      <CommandBinding Command="{x:Static local:WorkOrderDetailsTreeViewModel._SetEditorFocus}" 
                     Executed="_SetEditorFocusExecuted"/>
      </UserControl.CommandBindings>


In Code Behind:
public static RoutedCommand _SetEditorFocus = new RoutedCommand();
public ConstructoyOfTheClass()
{
_SetEditorFocus.InputGestures.Add(new KeyGesture(Key.F5, ModifierKeys.None));
}



WPF - Command Binding


Example how to Create CommandBindingin in WPF:
Command Binding in WPF working like Bubbles and will stop
only if: e.Handled = true, if e.Handled = false Command will continue finding all VisualTree untill root element.
XAML:
<UserControl.CommandBindings>
  <CommandBinding Command="Save" 
                  CanExecute="SaveCanExecute" 
                 Executed="SaveExecuted">            
  </CommandBinding>
  <CommandBinding Command="{x:Static local:WorkOrderDetailsTreeViewModel._SetEditorFocus}" 
                  Executed="_SetEditorFocusExecuted"/>
</UserControl.CommandBindings>


C#:
private void SaveCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }
 
        private void SaveExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            //TODO;
        }        



WPF - Manually Update Binding Source


When we use binding, we can couple of ways of data binding: 

* OneWay
* Two Way
* OneWayToSource
OneTime


And, there are several UpdateSourceTriggers:

* LostFocus
* PropertyChanged
* Explicit

You can find all details information here: http://msdn.microsoft.com/en-us/library/ms752347.aspx#basic_data_binding_concepts

XAML for example:

<ComboBox Name="comboHour" ItemStringFormat="HH:mm" 
                           SelectedValue="{Binding EnteredOnTime, 
                                           Mode=TwoWay, 
                                           UpdateSourceTrigger=Explicit}" 
                           Background="#0E0000FF"  
                           Width="70" 
                           HorizontalAlignment="Left"/>



public static void UpdateItemSource(FrameworkElement item)
        {
            FieldInfo[] infos = item.GetType().GetFields(BindingFlags.Public | BindingFlags.Fl            attenHierarchy | BindingFlags.Instance | BindingFlags.Static);
            foreach (FieldInfo field in infos)
            {
                if (field.FieldType == typeof(DependencyProperty))
                {
                    DependencyProperty dp = (DependencyProperty)field.GetValue(null);
                    BindingExpression ex = item.GetBindingExpression(dp);
                    if (ex != null)
                    {                        
                      if(ex.ParentBinding.UpdateSourceTrigger == UpdateSourceTrigger.Explicit)
                         ex.UpdateSource();
                    }
                }
            }
          
            int count = VisualTreeHelper.GetChildrenCount( item);
            for (int i = 0; i < count; i++)
            {
              FrameworkElement child = VisualTreeHelper.GetChild(item, i) as FrameworkElement;
                if(child != null)
                    UpdateItemSource(child);
            }
        }    

יום רביעי, 13 באפריל 2011

WPF - VisualTreeHelper - FindChild / FindParent Methods Examples

Find Child:

private DependencyObject FindChildControl<T>(DependencyObject control)
{
  int childNumber = VisualTreeHelper.GetChildrenCount(control);
  for (int i = 0; i < childNumber; i++)
  {
    DependencyObject child = VisualTreeHelper.GetChild(control, i);
    if (child != null && child is T)
        return child;
    else
        FindChildControl<T>(child);                
  }
  return null;
}
Find Parent:
private DependencyObject FindParentControl<T>(DependencyObject control)
{
   DependencyObject parent = VisualTreeHelper.GetParent(control);
   while (parent != null && !(parent is T))
   {
       parent = VisualTreeHelper.GetParent(parent);
   }        
   return parent;
}
Enjoy ! 

יום שני, 11 באפריל 2011

WPF - ObservableCollection via BindingList



Very important question, witch data structure to use when we working with WPF  ObservableCollection or BindingList?

1.      The practical difference is that BindingList is for WinForms, and ObservableCollection is for WPF.
From a WPF perspective, BindingList isnt properly supported, and you would never really use it in a WPF project unless you really had to.

but...

2.      ObservableCollection implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated.
However, BindingList implements IBindingList.
IBindingList provides notification on collection changes, but not only this. It provides a whole bunch of functionality which can be used by the UI to provides a lot more thing than only UI updates according to changes, like:
·         Sorting
·         Searching
·         Add through factory (AddNew member function).
·         Readonly list (CanEdit property)
All these functionalities are not available in ObservableCollection
Note that in Silverlight, BindingList is not available as an option: You can however use ObservableCollections and ICollectionView (and IPagedCollectionView if I remember well).

The choice is yours :-)

Thanks.

יום ראשון, 10 באפריל 2011

WPF - Set


Just example of creating Style for specific Control Type, 
but with very IMPORTANT tip:

Don't specify x:Key value - it's doesn't work:

<UserControl.Resources>
        <Style TargetType="{x:Type Label}">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Background" Value="Transparent"/>                    
        </Style>
    </UserControl.Resources>
    
    <StackPanel Name="MainStackPanel">
        <Grid x:Name="CondensedPanel" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.02*"/>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Label Grid.Column="1" x:Name="txtCondensed" Content="שם: "></Label>
            <Label Grid.Column="2" x:Name="lblCondensed" Content="{Binding ld_name}"></Label>
            ....

Thank You,
Enjoy! 

WPF - Find Parent Control in VisualTree - Generics Methods

If you need to find parent control, but you would like to use generic method for find one, you can use
method I wrote:


public UIElement GetParent<TSource>(UIElement element, UIElement stopOn)
{
   while (element != null && !(element is TSource) && (element != stopOn))
   {
      element = VisualTreeHelper.GetParent(element) as UIElement;               
   }
   if (element == stopOn)
      return null;
   else
      return element;
}
and you can implement it like this:
UIElement expander = GetParent<Expander>(element, parent);
if ( expander != null)
     return;
P.S.
Its very helpful if you are using routed events of WPF (instance:PreviewMouseLeftButtonDown)
and you want to stop bubble events on one of control on the way.

יום חמישי, 7 באפריל 2011

WPF Data Binding Converter


Following Post explains how to create Converter for WPF data binding.
For example we need to bind Lable control to his Background propertie:


 <Label.Background>
                    <Binding Path="BranchColor">...

but in Data Source we are getting string instead of Color: Red, Blue, Green and so...

We need to create converter that will possible to convert string "colors" to System.Windows.Model class.

We need to perform following steps:



1. Write Converter class with converts and Converts back methods:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media;
using System.Drawing;

namespace GarageManager.Converters
{  
    [ValueConversion(typeof(decimal), typeof(string))]
    public class BrushesConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;
            BrushConverter convertor = new BrushConverter();
            return convertor.ConvertFromString(value.ToString());
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;
            BrushConverter convertor = new BrushConverter();
            return convertor.ConvertToString(value);
        }
    }
}

2. Register it on our XML file where we writing data binding:

xmlns:local="clr-namespace:GarageManager.Converters"

3. and perform data biding with converter:

 <Label x:Name="lblBranchColor"  Grid.Column="0" Grid.RowSpan="3" Width="5">
                <Label.Background>
                    <Binding Path="BranchColor">
                        <Binding.Converter>
                            <local:BrushesConverter></local:BrushesConverter>
                        </Binding.Converter>
                    </Binding>
                </Label.Background>
            </Label>

Hope it's HELPS,
Thanks,
Alex.

יום ראשון, 3 באפריל 2011

Add Double Click Event on TreeNode in Style


Followin code adding MouseDoubleClick Event to TreeView control in Style tag of XAML.
I think it' clear enough without explanations: 

 <TreeView Grid.Row="1"    
  x:Name="MainTreeView"
  Background="Transparent"
  HorizontalAlignment="Stretch"
  ItemsSource="{Binding List}"
  VerticalAlignment="Stretch" 
  ItemTemplate="{StaticResource ClassificationTemplate}" 
                 <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <EventSetter Event="MouseDoubleClick" Handler="OnTreeNodeDoubleClick"/>
                    </Style>
                 </TreeView.ItemContainerStyle>
 </TreeView>


Code Behind:

private void OnTreeNodeDoubleClick(object sender, MouseButtonEventArgs mouseEvtArgs)
{
   .....add your code here                    
}

Find String in WPF TreeView control


How to find string in TreeView control?
The biggest thing about WPF is data binding and so in WPF TreeView very important to what source you binded your TreeView.
For example my source will 3 hierarchical classes A, B and C:

public class A
{
   public string Filed {get; set;}
   List<B> bList = new List<B>();
}

public class B
{
   public string Filed {get; set;}
   List<C> bList = new List<C>();
}

public class C
{
   public string Filed {get; set;}
}


public void Main()
{
   A a = new A();
   B b = new B();
B bb = new B();
C c = new C();
C cc = new C();
C ccc = new C();
a.bList.Add(b);
a.bList.Add(bb);
b.cList.Add(c);
b.cList.Add(cc);
b.cList.Add(ccc);
}


And in XAML we created HierarchicalDataView with relevant DataBinding and got TreeView:

<HierarchicalDataTemplate x:Key="elementTemplate">   
 <StackPanel Orientation="Horizontal">
  <TextBlock Text="{Binding cList}"/>
    <TextBlock Text="{Binding NormaTimeCount, StringFormat= - כמות: {0}}"/>
 </StackPanel>
 </HierarchicalDataTemplate>
 <HierarchicalDataTemplate x:Key="chapterTemplate" 
   ItemsSource="{Binding Path=bList}"                   
   ItemTemplate="{StaticResource elementTemplate}">
   <Grid>
    <Grid.RowDefinitions>
    <RowDefinition></RowDefinition>
    <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
    <ColumnDefinition></ColumnDefinition>
    <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <TextBlock Text="{Binding ld_name}" Grid.Column="0" Grid.Row="0" />
   </Grid>
 </HierarchicalDataTemplate>
 <HierarchicalDataTemplate x:Key="ClassificationTemplate"                            ItemsSource="{Binding Path=Chapters.List}"                   
   ItemTemplate="{StaticResource chapterTemplate}">
   <Grid>
    <Grid.RowDefinitions>
    <RowDefinition></RowDefinition>
    <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition></ColumnDefinition>
    <ColumnDefinition></ColumnDefinition>
    <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <TextBlock Text="{Binding ld_name}" Grid.Column="0" Grid.Row="0" />
  </Grid>
  </HierarchicalDataTemplate>

C# code:

private void btnFind_Click(object sender, RoutedEventArgs e)
{
   Find(_MainTreeView, textToFindt);
}

private void Find(TreeView mainTreeView, string findToText)
{
   foreach (object item in mainTreeView.Items)
   {
     TreeViewItem treeItem = mainTreeView.ItemContainerGenerator.ContainerFromItem(item) 
                                                                          as TreeViewItem;
                if (treeItem != null)
                    FindAll(treeItem, findText);
                if (item != null)
                {
                    dynamic obje = treeItem.Header;
                    if (isContains(obje.ld_name, findText))
                    {
                        treeItem.Focus();
                        treeItem.IsExpanded = true;
                        treeItem.Background = Brushes.Red;
                    }
                }
            }
        }
 
void FindAll(ItemsControl items, string textToFind)
        {
            foreach (object obj in items.Items)
            {
                ItemsControl childControl = items.ItemContainerGenerator.ContainerFromItem(obj) as ItemsControl;
                if (childControl != null)
                {
                    FindAll(childControl, findText); // Recursion
                }
                TreeViewItem item = childControl as TreeViewItem;
                if (item != null)
                {
                     //only if you dont know type of your Tree (in our case it's A, B or C and you need to use if or switch                        - case statement)
                    dynamic obje = item.Header;                     if (isContains(obje.ld_name, findText))                     {                         item.Focus();                         item.IsExpanded = true; // if you need to expand and show it                         item.Background = Brushes.Red;                     }                 }             }         }