Tuesday, September 15, 2009

LinkButton in Silverlight DataGrid

Here i will show how you can show linkButton in silverlight2 DataGrid:--

For my Project I needed to show a column as a link button. for implementing this i first tried using standard linkButton in DataTemplate:

XAML Markup:


<datatemplate key="BudgetTemplate">
<border borderthickness="5,0,0,0">
<hyperlinkbutton content="{Binding Budget}">
</hyperlinkbutton>
</border>
</DataTemplate>



then set the CelTemplate in DataGrid's Autogenaration Column event-
Code:
if(e.PropertyName == "Budget")
{
DataGridTemplateColumn budgetColumn = new DataGridTemplateColumn();
budgetColumn.Header = "Budget";
budgetColumn.CellTemplate = (DataTemplate)Resources["BudgetTemplate"];
e.Column = budgetColumn;
}

But after doing this it was not working properly. like when im going another page by clicking the hyperlinkButton then back agin this page the seleced row was hidden/blur for some reason. and there was no underline in the Hyperlinkbuttn when mouse hover.

the i tried in little different ways. i use a textblock instead of a linkbutton:


XMAL Markup:

<DataTemplate x:Key="BudgetTemplate">
<Border BorderThickness="1.5,0,0,0" BorderBrush="DarkGray">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="budgetTextBlock" Foreground="#FF065091" Width="auto" MouseLeftButtonUp="budgetTextBlock_MouseLeftButtonUp" MouseEnter="budgetTextBlock_MouseEnter" MouseLeave="budgetTextBlock_MouseLeave"
Text="{Binding Budget}" Cursor="Hand"
Margin="2,0,5,0" TextAlignment="Right" HorizontalAlignment="Right"/>

</StackPanel>

</Border>
</DataTemplate>


And set the CelTemplate in the same ways. for showing the underline i use the mouse Enter and MouseLeave event:

private void budgetTextBlock_MouseEnter(object sender, MouseEventArgs e)
{
((TextBlock)sender).TextDecorations = TextDecorations.Underline;
((TextBlock)sender).FontWeight = FontWeights.SemiBold;
}


private void budgetTextBlock_MouseLeave(object sender, MouseEventArgs e)
{
((TextBlock)sender).TextDecorations = null;
((TextBlock)sender).FontWeight = FontWeights.Normal;
}


And it worked fine for me.

5 Strategies for Getting More Work Done in Less Time

Summary.    You’ve got more to do than could possibly get done with your current work style. You’ve prioritized. You’ve planned. You’ve dele...