WPF controls intercepting Undo/Redo (C#)

June 29th, 2010

If you want to implement your own Undo/Redo and prevent e.g. a TextBox from intercepting, attach to the command preview events through CommandManager.AddPreviewCanExecuteHandler and CommandManager.AddPreviewExecutedHandler and set the event.Handled flag to true:

MySomething()
{
    CommandManager.AddPreviewCanExecuteHandler(
        this,
        new CanExecuteRoutedEventHandler(OnPreviewCanExecuteHandler));
    CommandManager.AddPreviewExecutedHandler(
        this,
        new ExecutedRoutedEventHandler(OnPreviewExecutedEvent));
}
void OnPreviewCanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{
    if (e.Command == ApplicationCommands.Undo)
    {
        e.CanExecute = true;
        e.Handled = true;
    }
    else if (e.Command == ApplicationCommands.Redo)
    {
        e.CanExecute = true;
        e.Handled = true;
    }
}
void OnPreviewExecutedEvent(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Command == ApplicationCommands.Undo)
    {
        // DO YOUR UNDO HERE
        e.Handled = true;
    }
    else if (e.Command == ApplicationCommands.Redo)
    {
        // DO YOUR REDO HERE
        e.Handled = true;
    }
}

Convert DOS wildcards to Regex search pattern (C#)

May 8th, 2010

Based on Codekeep by Stephen Smith. Additionally I escape backslash:

public static string WildcardToRegex(string pattern)
{
    return ("^" +
         pattern.Replace(@"\", @"\\")
         .Replace(".", @"\.")
         .Replace("*", "(.*)")
         .Replace("?", "(.{1,1})"));
        }
}

Here some testcode:

string[] texts = new string[]
{
    @"\\VirtualDrive\Test1.mp3",
    @"\\VirtualDrive\Test2.mp3",
    @"\\VirtualDrive\Test3.bin"
};

string wildCards = @"\\VirtualDrive\*.mp3";
string pattern = WildcardToRegex(wildCards);

string[] result =
    (from t in texts
    where RegularExpressions.Regex.IsMatch(t, pattern, RegexOptions.IgnoreCase)
    select t).ToArray();

UnitTest.Test(result.Length == 2);
UnitTest.Test(result[0] == texts[0]);
UnitTest.Test(result[1] == texts[1]);

WPF Startingpoints

March 13th, 2010

Essential Windows Presentation Foundation by Chris Anderson.

MSDN: Walkthrough: Create a Button by Using XAML

Sacha Barber’s ‘WPF: A Beginner’s Guide – Part 1 of n’ at Codeproject
Christian Moser’s page www.wpftutorial.net
MSDN Social: WPF FAQ

WPF ListView Example

March 12th, 2010

I am far away knowing how things work in WPF. For writing an own list view I needed quite much time. If it is done right? I can’t tell. Here how I understood things: Demo Project for Visual Studio 2008.

The goals were:

  1. Data is provided through data binding
  2. Support for different type of objects, each getting its own view
  3. Overwrite the default look with the blue selection background

A data bound WPF ListView in XAML with overwritten look using templates and template triggers:

<ListView Name="myListView" ItemsSource="{Binding MyList}">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="Border" Padding="2"
                                SnapsToDevicePixels="true"
                                Background="Transparent">
                            <ContentPresenter Name="Content" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Border"
                                Property="Background" Value="Tomato"/>
                                <Setter Property="Foreground" Value="Yellow"/>
                                <Setter Property="FontWeight" Value="Bold" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="Gray"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="FontWeight" Value="Bold" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.Resources>
        <DataTemplate DataType="{x:Type local:MyItemBase}">
            <TextBlock Text="{Binding Path=Name}"></TextBlock>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:MyItemDerived}">
            <StackPanel>
                <TextBlock Text="{Binding Path=Name}"></TextBlock>
                <TextBlock Text="{Binding Path=Description}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListView.Resources>
</ListView>

Here the document classes in c#. MyDocument contains the list. MyItemBase and MyItemDerived are the classes of items contained in the list. For both item classes a data template was defined in the XAML file:

public class MyItemBase
{
    private string name;

    public MyItemBase(string name)
    { Name = name; }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

public class MyItemDerived : MyItemBase
{
    private string description;

    public MyItemDerived(string name, string description)
        : base(name)
    { Description = description; }

    public string Description
    {
        get { return description; }
        set { description = value; }
    }
}

public class MyItemList
{
    private ObservableCollection<MyItemBase> myList
        = new ObservableCollection<MyItemBase>();

    public MyItemList()
    {
        myList.Add(new MyItemBase("Banana"));
        myList.Add(new MyItemBase("Apple"));
        myList.Add(new MyItemDerived("Orange", "Also a color"));
        myList.Add(new MyItemBase("Mango"));
    }

    public ObservableCollection<MyItemBase> MyList
    {
        get { return myList; }
    }
}

Here the view gets connected with the document:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        myListView.DataContext = new MyItemList();
    }
}

And here is how it looks in action (quite bad, eh?):

Screenshot of TestListView

References:

MDSN: ListView
MSDN: ListViewItem
MSDN: How to ListView

Codeproject: WPF custom ListBox with scrollbar on the background
SwitchOnTheCode: ListView tutorial with editable GridView

Blog: About ScrollViewer.CanContentScroll
Blog: Get rid of blue background 1
Blog: Get rid of blue background 2

Windows UAC virtualization

February 10th, 2010

MSDN: New UAC Technologies for Windows Vista
MSDN: Registry Virtualization

You can verify the status of Virtualization in Task Manange / View / Select Columns / show the Virtualization column.

Replace my CD collection by hard disc

February 3rd, 2010

For now, this one seems to be the winner:
CDs digitalisieren lassen Eins, Zwei, MP3… (SERVICE)

Audio-CDs rippen mit Exact Audio Copy (HI CONTENT QUALITY, DO IT YOURSELF)

Digitalisierung von Audio-Archiven: Aspekte, Probleme, Verfahrensweisen (HI CONTENT QUALITY, DO IT YOURSELF)

Windows Virtual Memory

February 3rd, 2010

MSDN: The Virtual-Memory Manager in Windows NT (1992)
MSDN: Managing Virtual Memory in Win32 (1993)
MDSN: Managing Heap Memory in Win32 (1993)
MSDN: Managing Memory-Mapped Files in Win32 (1993)

MSDN: Entry point up-to-date memory management

MSDN: Memory Performance Information
MSDN Example: Collecting Memory Usage Information For a Process

MSDN: Memory Limits for Windows Releases
MSDN: 4-Gigabyte Tuning

MSDN: Memory Management Functions

Technet: Understanding Memory Usage in Windows 2000

Forum discussion about Retrieving memory usage

Windows Structured Exception Handling

January 18th, 2010

MSDN: About Structured Exception Handling. Coarse overview.

Matt Pietrek: A Crash Course on the Depths of Win32 Structured Exception Handling (1997). Telling in detail how structured exception handling works on machine level and how compilers implement them on x86.

Matt Pietrek: New Vectored Exception Handling in Windows XP (2001). Telling how vectored exception handling works that came with Windows XP.

cbrumme’s WebLog: Windows SEH. The Exception Model, Windows SEH, Managed Exceptions, Flow Control, Performance and Trends…

Additional:
MSDN: AddVectoredExceptionHandler
MSDN: RemoveVectoredExceptionHandler
MSDN: Example: Using a Vectored Exception Handler
MSDN: Smart Device Development: SEH in x86 Environments
MSDN: _set_se_translator
MSDN: EXCEPTION_POINTERS
MSDN: EXCEPTION_RECORD

Mark Russinovich on Windows 7 system changes

December 17th, 2009

From PDC 09: Part 1, Part 2, each 1.30 hours.

How to disable IntelliSense in VC2008

October 16th, 2009

Jim Springfield wrote a good starting point how to disable IntelliSense via VS macros: http://blogs.msdn.com/vcblog/…