Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
FormattedTitle feature added
Browse files Browse the repository at this point in the history
  • Loading branch information
akgulebubekir committed Apr 6, 2017
1 parent 20f0c36 commit 3d27628
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
103 changes: 55 additions & 48 deletions DataGridSample/DataGridSample/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,63 @@
xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
xmlns:conv="clr-namespace:DataGridSample.Views.Converters;assembly=DataGridSample"
>
<ContentView BackgroundColor="White" Padding="20">
<ContentView BackgroundColor="White" Padding="20">


<dg:DataGrid ItemsSource="{Binding Teams}" SelectionEnabled="True" SelectedItem="{Binding SelectedTeam}"
RowHeight="70" HeaderHeight="50" BorderColor="#CCCCCC" HeaderBackground="#E0E6F8"
PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}"
ActiveRowColor="#8899AA">
<dg:DataGrid.HeaderFontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Tablet>15</OnIdiom.Tablet>
<OnIdiom.Phone>12</OnIdiom.Phone>
</OnIdiom>
</dg:DataGrid.HeaderFontSize>
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Logo" PropertyName="Logo" Width="100" SortingEnabled="False">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Aspect="AspectFit" HeightRequest="60" />
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
<dg:DataGridColumn Title="Team" PropertyName="Name" Width="2*"/>
<dg:DataGridColumn Title="Win" PropertyName="Win" Width="0.95*"/>
<dg:DataGridColumn Title="Loose" PropertyName="Loose" Width="1*"/>
<dg:DataGridColumn Title="Home" PropertyName="Home"/>
<dg:DataGridColumn Title="Percentage" PropertyName="Percentage" StringFormat="{}{0:0.00}" />
<dg:DataGridColumn Title="Streak" PropertyName="Streak" Width="0.7*">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<ContentView HorizontalOptions="Fill" VerticalOptions="Fill"
BackgroundColor="{Binding Converter={StaticResource StreakToColorConverter}}">
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" TextColor="Black"/>
</ContentView>
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
</dg:DataGrid.Columns>
<dg:DataGrid.RowsBackgroundColorPalette>
<dg:PaletteCollection>
<Color>#F2F2F2</Color>
<Color>#FFFFFF</Color>
</dg:PaletteCollection>
</dg:DataGrid.RowsBackgroundColorPalette>
<dg:DataGrid.Resources>
<ResourceDictionary>
<conv:StreakToColorConverter x:Key="StreakToColorConverter"/>
</ResourceDictionary>
</dg:DataGrid.Resources>
</dg:DataGrid>
</ContentView>
<dg:DataGrid ItemsSource="{Binding Teams}" SelectionEnabled="True" SelectedItem="{Binding SelectedTeam}"
RowHeight="70" HeaderHeight="50" BorderColor="#CCCCCC" HeaderBackground="#E0E6F8"
PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}"
ActiveRowColor="#8899AA">
<dg:DataGrid.HeaderFontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Tablet>15</OnIdiom.Tablet>
<OnIdiom.Phone>12</OnIdiom.Phone>
</OnIdiom>
</dg:DataGrid.HeaderFontSize>
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Logo" PropertyName="Logo" Width="100" SortingEnabled="False">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Aspect="AspectFit" HeightRequest="60" />
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
<dg:DataGridColumn Title="Team" PropertyName="Name" Width="2*"/>
<dg:DataGridColumn Title="Win" PropertyName="Win" Width="0.95*"/>
<dg:DataGridColumn Title="Loose" PropertyName="Loose" Width="1*"/>
<dg:DataGridColumn PropertyName="Home">
<dg:DataGridColumn.FormattedTitle>
<FormattedString>
<Span Text="Home" ForegroundColor="Black" FontSize="13" FontAttributes="Bold"/>
<Span Text=" (win-loose)" ForegroundColor="#333333" FontSize="11" />
</FormattedString>
</dg:DataGridColumn.FormattedTitle>
</dg:DataGridColumn>
<dg:DataGridColumn Title="Percentage" PropertyName="Percentage" StringFormat="{}{0:0.00}" />
<dg:DataGridColumn Title="Streak" PropertyName="Streak" Width="0.7*">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<ContentView HorizontalOptions="Fill" VerticalOptions="Fill"
BackgroundColor="{Binding Converter={StaticResource StreakToColorConverter}}">
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" TextColor="Black"/>
</ContentView>
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
</dg:DataGrid.Columns>
<dg:DataGrid.RowsBackgroundColorPalette>
<dg:PaletteCollection>
<Color>#F2F2F2</Color>
<Color>#FFFFFF</Color>
</dg:PaletteCollection>
</dg:DataGrid.RowsBackgroundColorPalette>
<dg:DataGrid.Resources>
<ResourceDictionary>
<conv:StreakToColorConverter x:Key="StreakToColorConverter"/>
</ResourceDictionary>
</dg:DataGrid.Resources>
</dg:DataGrid>
</ContentView>
</ContentPage>


Expand Down
35 changes: 14 additions & 21 deletions Xamarin.Forms.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public partial class DataGrid : Grid
self.InternalItems = new List<object>(((IEnumerable)n).Cast<object>());
}
if (self.NoDataView != null)
{
if (self.ItemsSource == null || self.InternalItems.Count() == 0)
self._noDataView.IsVisible = true;
else if (self._noDataView.IsVisible)
self._noDataView.IsVisible = false;
}
if (self.NoDataView != null)
{
if (self.ItemsSource == null || self.InternalItems.Count() == 0)
self._noDataView.IsVisible = true;
else if (self._noDataView.IsVisible)
self._noDataView.IsVisible = false;
}
});

void HandleItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
Expand Down Expand Up @@ -182,7 +182,7 @@ void HandleItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEv
});
#endregion

#region properties
#region Properties
public Color ActiveRowColor
{
get { return (Color)GetValue(ActiveRowColorProperty); }
Expand Down Expand Up @@ -364,7 +364,7 @@ public View NoDataView
}
#endregion

#region fields
#region Fields

Dictionary<int, SortingOrder> _sortingOrders;

Expand Down Expand Up @@ -402,15 +402,11 @@ protected override void OnParentSet()
}
#endregion

#region header creation methods
#region Header Creation Methods

private View GetHeaderViewForColumn(DataGridColumn column)
{
column.HeaderLabel = new Label
{
Text = column.Title,
Style = column.HeaderLabelStyle ?? this.HeaderLabelStyle ?? (Style)_headerView.Resources["HeaderDefaultStyle"]
};
column.HeaderLabel.Style = column.HeaderLabelStyle ?? this.HeaderLabelStyle ?? (Style)_headerView.Resources["HeaderDefaultStyle"];

Grid grid = new Grid
{
Expand All @@ -422,10 +418,7 @@ private View GetHeaderViewForColumn(DataGridColumn column)

if (IsSortable)
{
column.SortingIcon = new Image
{
Style = (Style)_headerView.Resources["ImageStyleBase"],
};
column.SortingIcon.Style = (Style)_headerView.Resources["ImageStyleBase"];

grid.Children.Add(column.SortingIcon);
Grid.SetColumn(column.SortingIcon, 1);
Expand Down
18 changes: 17 additions & 1 deletion Xamarin.Forms.DataGrid/DataGridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public class DataGridColumn : BindableObject, IDefinition
propertyChanged: (b, o, n) => { if (o != n) (b as DataGridColumn).OnSizeChanged(); });

public static readonly BindableProperty TitleProperty =
BindableProperty.Create(nameof(Title), typeof(string), typeof(DataGridColumn), string.Empty);
BindableProperty.Create(nameof(Title), typeof(string), typeof(DataGridColumn), string.Empty,
propertyChanged: (b, o, n) => (b as DataGridColumn).HeaderLabel.Text = (string)n);

public static readonly BindableProperty FormattedTitleProperty =
BindableProperty.Create(nameof(FormattedTitle), typeof(FormattedString), typeof(DataGridColumn),
propertyChanged: (b, o, n) => (b as DataGridColumn).HeaderLabel.FormattedText = (FormattedString)n);

public static readonly BindableProperty PropertyNameProperty =
BindableProperty.Create(nameof(PropertyName), typeof(string), typeof(DataGridColumn), null);
Expand Down Expand Up @@ -54,6 +59,11 @@ public string Title
set { SetValue(TitleProperty, value); }
}

public FormattedString FormattedTitle
{
get { return (string)GetValue(FormattedTitleProperty); }
set { SetValue(FormattedTitleProperty, value); }
}
public string PropertyName
{
get { return (string)GetValue(PropertyNameProperty); }
Expand Down Expand Up @@ -103,6 +113,12 @@ public Style HeaderLabelStyle

public event EventHandler SizeChanged;

public DataGridColumn()
{
HeaderLabel = new Label();
SortingIcon = new Image();
}

void OnSizeChanged()
{
SizeChanged?.Invoke(this, EventArgs.Empty);
Expand Down

0 comments on commit 3d27628

Please sign in to comment.