How to create a shortcut file with .NET

Unfortunately .NET doesn't have direct support for this task but luckily you can leverage Interop and the Windows Scripting Host Object Model to do this with only a few lines of code. Windows Scripting Host is an automation technology for Microsoft Windows operating systems that provides scripting capabilities comparable to batch files, but with a greater range of supported features.


Steps to get this working:

1) Add a reference to "Windows Script Host Object Model" to your project - this will be found under the COM tab in the add reference dialog.

2) Add a using to the class where your shortcut create code will go. e.g.

using IWshRuntimeLibrary;

3) Use the following code to create a shortcut. I would recommend making a helper class for this. e.g.

    WshShell shell = new WshShell();
    IWshShortcut link = (IWshShortcut)shell.CreateShortcut("c:\My Notepad Shortcut.lnk");
    link.TargetPath = "c:\windows\notepad.exe";
    link.Save();

Why doesn't my keyboard navigation work on templated ComboBoxes/ItemControls

There is a little gotcha here when you overwrite the itemcontainerstyle on an items control. If you want to allow quick access items by typing prefixes of strings you need to setup some attached properties.

There is 2 ways you can go you can either add the TextSearch.TextPath attached property on the ItemControl or add the TextSearch.Text attached property on the individual item.

Example:

<ComboBox IsEditable="true" TextSearch.TextPath="Name">
            <Image Name="Cat" Source="data\cat.png"/>
            <Image Name="Dog" Source="data\dog.png"/>
            <Image Name="Fish" Source="data\fish.png"/>
ComboBox>

<ComboBox IsEditable="true">
<Image TextSearch.Text="Cat" Source="data\cat.png"/>
            <Image TextSearch.Text="Dog" Source="data\dog.png"/>
            <Image TextSearch.Text="Fish" Source="data\fish.png"/>
ComboBox>


Example of how to do this when overwriting the itemcontainerstyle:

        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="TextSearch.Text">
                    <Setter.Value>
                        <MultiBinding StringFormat="{} {0} {1}">
                            <Binding Path="FirstName"/>
                            <Binding Path="LastName"/>
                        MultiBinding>
                    Setter.Value>
                Setter>
            Style>
        ComboBox.ItemContainerStyle>
  


How to deal with "CMD does not support UNC paths as current directories"

You need to use the "pushd" command instead of "cd" to change the current directory to a UNC path (e.g.: >pushd \\servername\sharename).

"the pushd command creates a temporary drive letter that points to the network resource, and then changes the current drive and folder to the new drive letter. Temporary drive letters are allocated starting from Z and then backward through the alphabet, using the first unused drive letter found."

Use "popd" when you're finished (e.g.: >popd) to cleanup the temporary mapped drive.

For more info see How To Use the PUSHD Command to Access a UNC Path at a Command Prompt in Windows 2000 (http://support.microsoft.com/kb/317379)

OR

Use powershell - which has complete support for unc paths.

How to iterate over the rows and cells in an Xceed WPF DataGrid

I found this handy code on the Xceed forums today. See http://xceed.com/CS/forums/permalink/26496/26518/ShowThread.aspx#26518

            foreach (object item in thisDataGrid1.Items)
            {
                Dispatcher.BeginInvoke(new Action<object>(DoRow), DispatcherPriority.ApplicationIdle, item);
            }

        private void DoRow(object item)
        {
            Xceed.Wpf.DataGrid.DataRow row = this.DataGrid1.GetContainerFromItem(item) as Xceed.Wpf.DataGrid.DataRow;
            if (row != null)
            {
                foreach (Xceed.Wpf.DataGrid.DataCell c in row.Cells)
                {
                    if (c != null)
                    {
                        //Do something to the cell
                    }
                }
            }
        }

What is a first chance exception anyway?

This kb article is the best explanation of what a first chance exception is I have ever read (http://support.microsoft.com/kb/105675):

“However, if the application is being debugged, the debugger sees all exceptions before the program does. This is the distinction between the first and second chance exception: the debugger gets the first chance to see the exception (hence the name). If the debugger allows the program execution to continue and does not handle the exception, the program will see the exception as usual. If the program does not handle the exception, the debugger gets a second chance to see the exception. In this latter case, the program normally would crash if the debugger were not present.”

How do I catch first chance exceptions?


In visual studio under the debug > exceptions menu and tick the boxes shown in red:

How do you use a converter on a trigger


You can’t.

You can however use a data trigger and set the binding RelativeSource to Self. Data Triggers allow binding and bindings lets you have converters. Yey

Example:

       <Button Content="I change colour depending on my width for some reason">
            <Button.Triggers>
                <DataTrigger
                    Binding="{Binding
                    Path=Width,
                    RelativeSource={RelativeSource Self},
                    Converter={StaticResource isLessThanConverter},
                    ConverterParameter=50}"
                    Value="True">
                    <Setter Property="Button.Background" Value="Red" />
                DataTrigger>
            Button.Triggers>
        Button>

How to make Tooltips display on disabled WPF controls


This is old news but I find lots of people still don't know about this.

By default, WPF does not show tooltips on controls that have been disabled. To fix this, set the attached property ToolTipService.ShowOnDisabled.

E.g.
<Button Content="Example Button" 
 ToolTipService.ShowOnDisabled="True"  
 ToolTip="I’m visible even when the control is not enabled"/>

Extracting data from a resx file using c#

.Net has a built-in reader (ResxResourceReader) and writer (ResxResourceWriter) which makes it really easy to read a resx file. The classes can be found in the System.Resources namespace under the System.Windows.Forms assembly (in System.Windows.Forms.dll)  

Basic Reader Example:

 
            var rsxr = new ResXResourceReader("demo.resx");

            foreach (DictionaryEntry d in rsxr)
            {
                Console.WriteLine(pathRelativeToStartingPath + ",\"" + d.Key + "\",\"" + d.Value + "\"");
            }

            rsxr.Close();           
         
            
Basic Writer Example:


            string txt = "One smart fellow, he felt smart";
            string txt_fr = "Un garçon intelligent, il se sentait à puce";


            ResXResourceWriter rsxw = new ResXResourceWriter("demo.resx");
            rsxw.AddResource("MyText", txt);
            rsxw.Close();

            ResXResourceWriter rsxw_fr = new ResXResourceWriter("demo.fr-FR.resx");
            rsxw_fr.AddResource("MyText", txt_fr);
            rsxw_fr.Close();    
  
            
Of course if you are putting images or media into your resx file you'll additional code to handle the different types that value can be.

A quick way to delete all the bin and obj folders in your solution

Something like this is a cinch with Powershell

1. open the powershell command prompt
2. enter the command get-childitem -path "C:\sourceCode\MySolution" -recurse -include obj,bin | remove-item -force -recurse

Handy for ensuring your solution will build from scratch with no unreferenced/circular referenced assemblies or for deleting dlls that have become locked which sometimes happens. Same principle could easily be applied to the asp.net cache.

How to obtain access to nested types through XAML

Answer: "+" indicates nested type in XAML

For example I want to access the View member on the Shipping type which is nested inside the Setup type e.g.

public struct Setup
{
  public struct Shipping
  {
    public const string View = "This is the value I want to access";
  }
}

The syntax would be CommandParameter="{x:Static demoAlias:Setup+Shipping.View}"