Enum’s and MarkupExtension

Here is quick sample to get all the values from an enum using MarkupExtension. This returns a Dictionary

 public class EnumValuesExtension : MarkupExtension
    {
        public string EnumName { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IXamlTypeResolver xamlResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
            Type t = xamlResolver.Resolve(EnumName);
            return t.GetFields().Where(field => field.IsLiteral).ToDictionary(x => (int)x.GetValue(null), x => x.Name);
        }
    }

If we have a enum like below

 public enum Days
    {
        Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri
    }

We can use it in xaml like this. ‘local’ is the xmlns declaration pointing to the namespace

 <ComboBox Width=”150″ Height=”25″ DisplayMemberPath=”Value”
                  ItemsSource=”{local:EnumValues EnumName=’local:Days’}”/>

Advertisements

DataGrid random behavior when scrolling

I came across a post in the silverlight forums which shows the random behavior of the DataGrid. This happens obviously only when scrollbars are there and when we scroll. Wraping DataGridRowsPresenter in the Template of the DataGrid in a scrollviewer seems to get around this issue. As scrollviewer has some border margin etc. I styled it to remove all unnecessary stuff.

 <ScrollViewer Grid.ColumnSpan=”2″ Grid.Row=”1″ Style=”{StaticResource ScrollViewerStyle1}”>
                                        <sdk:DataGridRowsPresenter x:Name=”RowsPresenter” />
                                    </ScrollViewer>  

If you remove the style applied to the DataGrid in the sample you can clearly see the random behavior. You can download the sample here .

DataGrid and “No Records Found” message

Here is one way to display a message when we dont have any data returned

1. Grab the Template of the DataGrid from Blend

2. Modify the Template. All we do is find the RowsPresenter line
<sdk:DataGridRowsPresenter x:Name=”RowsPresenter” Grid.ColumnSpan=”2″ Grid.Row=”1″/>
and move it inside a Grid element, we add in the template in the same place.
The Grid has a TextBlock to show the message and Visibility is controlled by a Converter

Here is the relevant part of the Template

<sdk:DataGridColumnHeader x:Name=”TopLeftCornerHeader” Template=”{StaticResource TopLeftHeaderTemplate}” Width=”22″/>
<sdk:DataGridColumnHeadersPresenter x:Name=”ColumnHeadersPresenter” Grid.Column=”1″/>
<sdk:DataGridColumnHeader x:Name=”TopRightCornerHeader” Grid.Column=”2″ Template=”{StaticResource TopRightHeaderTemplate}”/>
<Rectangle x:Name=”ColumnHeadersAndRowsSeparator” Grid.ColumnSpan=”3″ Fill=”#FFC9CACA” Height=”1″ StrokeThickness=”1″ VerticalAlignment=”Bottom” Width=”Auto”/>
<!–<sdk:DataGridRowsPresenter x:Name=”RowsPresenter” Grid.ColumnSpan=”2″ Grid.Row=”1″/>–>
<Grid Grid.ColumnSpan=”2″ Grid.Row=”1″ >
<sdk:DataGridRowsPresenter x:Name=”RowsPresenter”/>
<TextBlock Text=”No Records found”
Visibility=”{Binding ElementName=RowsPresenter, Path=Children.Count, Converter={StaticResource noRecordsConverter}}”
HorizontalAlignment=”Center” VerticalAlignment=”Center”
FontSize=”12″/>
</Grid>

<Rectangle x:Name=”BottomRightCorner” Grid.Column=”2″ Fill=”#FFE9EEF4″ Grid.Row=”2″/>
<Rectangle x:Name=”BottomLeftCorner” Grid.ColumnSpan=”2″ Fill=”#FFE9EEF4″ Grid.Row=”2″/>
<ScrollBar x:Name=”VerticalScrollbar” Grid.Column=”2″ Margin=”0,-1,-1,-1″ Orientation=”Vertical” Grid.Row=”1″ Width=”18″/>

And the Converter

public class NoRecordsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString() == “0” ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}