Subscribe Now: Feed Icon

Thursday, May 31, 2012

Silverlight: TextBox binding only works when control not in focus

Yesterday I got to the point that most of my priority one tasks were done ahead of schedule so I have decided to go back to old controls and rewrite them to work with the MVVM pattern. I even got to an old argument with my co-worker Felix about using code behind for UI purposes (he is a firm believer in Expression Blend but for implementing Watermarks he decided to use code behind – I refactored it using this).

So after fixing the Watermark and the AutoCompleteBox that used a service to populate the items in the code behind (using this) I got down to the actual minor functionality tweaks. Like when the user presses enter in AutoCompleteBox or TextBox some Action is triggered. For the AutoCompleteBox I used the Attached Property Approach (I changed it a bit to be for KeyUp instead of KeyDown) just without the CommandArgument since the selected item was already with Data Binding. And it worked just fine.

But with TextBox this approach just didn’t work – because TextBox binds the data only when it is out of focus!

I found some more people with the same problem:

Go4Answers: TextBox TextChanged event doesn't cause data binding to update

Silverlight Forum: TextBox TextChanged event doesn't cause data binding to...

They used the behavior solution (which while browsing for solutions I missed (I guess I am turning lazy because in StackOverflow I would have gotten the right answer in the first page/answer)). I just decided to use the CommandArgument from Attached Property Approach (the same one I didn’t need for AutoCompleteBox):

<TextBox Text="{Binding Path=SomeText, Mode=TwoWay}" Components:EnterKeyUp.Command="{Binding Path=EnterSomeTextCommand}"
            Components:EnterKeyUp.CommandArgument="{Binding Path=Text,RelativeSource={RelativeSource Self}}">

The View Model:

public RelayCommand<string> EnterSomeTextCommand
{
    get;
    private set;
}

(using GalaSoft MvvmLight)

Now you can just use the value.

 

Update:

@yesez5 in the comments have added his blog post (in Spanish, or in English (using Google Translate)) about using a built-in behavior from the Prism Library (that way you don’t even have to add code implementations for Action triggers or Behaviors in your project).

 

Resources:

IBlog<Johan>: Silverlight Watermark TextBox Behavior

Peter Kuhn: Using the AutoCompleteBox

StackOverflow: Invoke Command When “ENTER” Key Is Pressed In XAML (and Answer)

Go4Answers: TextBox TextChanged event doesn't cause data binding to update

Silverlight Forum: TextBox TextChanged event doesn't cause data binding to...

Zoltan's Dev Corner: Binding update on TextBox.TextChanged event using Behaviors

GalaSoft MvvmLight

Lightside: Update TextBox binding when the value changes

Sunday, May 20, 2012

Default Browsers bug in VS2010

If you don't know (or haven't encountered it yet) there is a bug in VS that causes the settings of the default browser to not save.

For example: I am using Firefox as my default Windows browser but prefer debugging using IE (mainly because if IE dies my browser is still alive). And I have to change the browser much too often.

There isn't a fix for this but there is a workaround:

From VS -> Tools -> Extension Manager

VsExtensionManager

-> Online Gallery -> Search:

WoVS Default Browser Switcher

WoVSDefaultBrowserSwitcher

Download and install the extension and when prompt restart VS.

Now in VS: View -> Toolbar -> enable: Default Browser Switcher

ToolbarEnableDefaultBrowserSwitch

And you will get:

(image from WoVS Default Browser Switcher extension page)

And you will see what default browser is turned on.

DefaultBrowserSwitcherToolbar

I prefer putting it just below the debug toolbar so that it will be in front of my eyes but you can place it where ever you may like…

 

There is also a context menu for viewing web pages but it is not as useful as the toolbar.

You can read more about it here.

 

 

Resources:

Microsoft connect: VS2010 forgets default browser settings

Extension: WoVS Default Browser Switcher

Wednesday, May 9, 2012

A day without the Debugger

On Sunday morning I found out the Visual Studio 2010 Debugger has stopped working for me. It wasn’t isolated to a single project or technology. Attach to process didn’t work in Silverlight and running Unit Tests in debug mode gave the usual “The breakpoint will not be hit. No symbols have been loaded for this document.”:

BreakpointWillNotHit

Visual Studio was in Debug mode just didn’t pass through any breakpoint (just to check that the Unit Tests used the latest build I added Assert.Fail and the Unit Test failed).

So, first I thought to give myself a challenge and try to work without a debugger. All was of the good until I had a small and stupid bug that I was sure I could just solve using Unit Tests, but the server side worked and I could see the data passing from the client using Fiddler. It didn’t help that in the front of my mind I just knew that with a Debugger this will be solved in minutes. So I dropped the challenge and started investigating the problem.

 

First I went through the usual suspects:

 DebugInfoSettings

  • Restarted the computer
  • Enabled Just My Code (Options-> Debugging –> General), since I like having symbol debugging it was turned off
  • Reset the setting using Devenv.exe /resetsettings 
  • I decided against using Devenv.exe /SafeMode (but next time I will use it)

I was very close to trying the repair on the windows Control Panel. But then I noticed that while the debugger didn’t break on any breakpoint it did show a few BadImageFormatException exceptions being thrown, setting Visual Studio to break on those exceptions (Debug menu –> Exceptions… (or Ctrl+D+E) –> Find: “BadImageFormatException” and checking the thrown:

DebugExceptionThrown

Running it again:

System.BadImageFormatException occurred
  Message=Could not load file or assembly 'dbghelp.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
  Source=mscorlib
  FileName=dbghelp.dll
  FusionLog=""
  StackTrace:
       at System.Reflection.AssemblyName.nGetFileInformation(String s)
       at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
  InnerException: System.BadImageFormatException
       Message=Could not load file or assembly 'dbghelp.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
       FileName=dbghelp.dll
       FusionLog=""
       InnerException:

Or just the Dlls:

  • dbghelp.dll
  • DDConfig.dll
  • LanguagePackage.dll
  • Microsoft.VisualStudio.Coverage.Symbols.dll
  • Microsoft.VisualStudio.ProgressionIcons.dll
  • Microsoft.VisualStudio.QualityTools.RecorderBarBHO100.dll
  • pidgenx.dll
  • SqlTDiagN.dll
  • SqlToolsVSNativeHelpers.dll
  • symsrv.dll

Searching the file system for one of them dbghelp.dll,the icon for the DLLs was for dotPeek which got me thinking about uninstalling JetBrains early access tools dotPeek and dotCover (after closing VS) – just to be sure it wasn’t them…

Trying Debug again and everything worked.

 

The weird part is trying to reinstall those apps (with the exact same MSIs) didn’t reproduce the problem. Looking back I remember restarting my computer before all this started but even restart won’t reproduce the problem.

Another case for the “computer's nondeterministic nature”…

 

Resources:

Peter Gfader 's .NET Blog: VS2010 - Debugging not working

StackOverflow: debugging is not working in my visual studio 2010

MSDN Forums: Debugger not working on Visual Studio 2010