Showing posts with label Silverlight/WPF. Show all posts
Showing posts with label Silverlight/WPF. Show all posts

Thursday, May 5, 2011

Creating DataGridTemplateColumn from code behind

We can create DataGridTemplateColumn from code behind by defining a DataTemplate in the ResourceDictionary xaml and then use the DataTemplate to create the DataGridTemplateColumn.

In the below sample code, I am creating a data grid column with checkbox.

In the ResourceDictionary xaml, add DataTemplate for the checkbox:


<DataTemplate x:Key="CheckboxTemplate">
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
</DataTemplate>

From the code behind create the DataGridTemplateColumn for the checkbox:


DataGridTemplateColumn checkboxColumn = new DataGridTemplateColumn()
{
      CellTemplate = App.Current.Resources["CheckboxTemplate"] as DataTemplate
};

Double click issue with DataGridCheckbox column

DataGridCheckboxColumn in DataGrid seems to have double click issue. One has to click twice to check or uncheck, first click to select the row and the second click for the checkbox. We can use DataGridTemplateColumn instead for the checkbox which does not have double click issue. 

sample code:

<DataGridTemplateColumn Header="Name">
<controlsData:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding Path=IsSelected,Mode=TwoWay}" />
</DataTemplate>
</controlsData:DataGridTemplateColumn.CellTemplat>
</DataGridTemplateColumn>

Tuesday, January 25, 2011

Silverlight error: The debugger cannot continue running the process.

Today I got this error while debugging my silverlight application and the debugger just crashed without any useful error information. I got this error when the debugger crashed:

"The debugger cannot continue running the process. Process was terminated."

After doing some debugging and research I found this thread online where in many people had the same issue because of recursive call in class property:

http://social.msdn.microsoft.com/Forums/en-US/vsdebug/thread/f9b08dd4-8d20-4104-9a24-9f002caceb48/

I looked at my code and indeed I had a recursive call in one of my property by mistake.

private bool isUser;
public bool IsUser
{
get { return IsUser; }
set
{
IsUser = value;
OnPropertyChanged( "IsUser" );
}
}

As you can see in the above code my getter was recursively referring to IsUser property itself instead of the field isUser. This was just coding error but it took some time to figure out the root cause as the debugger was crashing without proper error information.

Tuesday, October 26, 2010

Date format in WPF/Silverlight

Here is an example for formatting date in WPF (Format: MM/DD/YYYY):

<TextBlock Text="{Binding CreationDate, StringFormat=\{0:MM/dd/yyyy\}}" />

Wednesday, September 22, 2010

Error when referencing assembly from network share - Silverlight 4 (.NET Framework 4.0)

I got this error today when I added reference to an assembly from network share:

"Error 3 Could not load the assembly file://\\CM\libraries\Microsoft\Rx\Reactive Extensions v1.0.2677.0\SL4\System.Observable.dll. This assembly may have been downloaded from the Web. If an assembly has been downloaded from the Web, it is flagged by Windows as being a Web file, even if it resides on the local computer. This may prevent it from being used in your project. You can change this designation by changing the file properties. Only unblock assemblies that you trust".

I am using Silverlight 4 (.NET Framework 4.0).

To fix this issue I added following entry in the Visual Studio 2010 devenv.config file.

< loadFromRemoteSources enabled="true" />
To the runtime section of:
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe.config