バージョン 20.1 (最新)

隣接サイズ変更のレスポンシブ レイアウト

このトピックでは、XamDataGrid コントロールの隣接レイアウトがサイズ変更される場合のレスポンシブ レイアウトの概念を解説します。

前提条件

本トピックの理解を深めるために、以下のトピックを参照することをお勧めします。

トピック 目的

このトピックでは、XamDataGrid コントロールをビューに追加し、サンプル データを生成するために必要な基本的な手順を紹介します。

このトピックでは、XamDataGrid コントロールの回転でレスポンシブ レイアウトを実装する方法について説明します。

概要

隣接するレイアウトの向きの変更により、アプリケーション内の XamDataGrid コントロールの幅を変更することが必要な場合があります。このようなシナリオでは、アプリケーションのデータの可読性と外観を維持する必要があります。この例では、XamDataGrid コントロールを使用して、このシナリオの条件に応答するレスポンシブ グリッド レイアウト ビヘイビアを実装するプロセスについて説明します。 このサンプルでは、応答に Button を使用して XamDataGrid コントロールの幅を変更し(隣接ビューの幅を変更)、ResponsiveState オブジェクトを適用して、優先度の低い列 (Territory 列および Sales 列) をビューで非表示にし、重要なデータ(Name 列)の可読性を維持します。

アプリケーションの準備

レスポンシブ レイアウト ビヘイビアを実装する前に、 XamDataGrid コントロールの ResponsiveState オブジェクトを定義するために後で使用されるアプリケーション プロジェクトを準備し、デバイスの画面のパラメーターを保存する必要があります。

  1. Portable アプリケーション プロジェクトで、このコードを追加して、デバイスの画面のパラメーターを定義します。

C# の場合:

public partial class App : Application
{
    static public int ScreenWidth;
    static public int ScreenHeight;
    static public float ScreenDensity = 1;
    ...
}
  1. .Droid アプリケーション プロジェクトで、このコードを追加して、デバイスの画面のパラメーターを保存します。

C# の場合:

public class MainActivity : FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        App.ScreenDensity = Resources.DisplayMetrics.Density;
        App.ScreenWidth   = Resources.DisplayMetrics.WidthPixels;
        App.ScreenHeight  = Resources.DisplayMetrics.HeightPixels;
        ...
    }
}
  1. .iOS アプリケーション プロジェクトで、このコードを追加して、デバイスの画面のパラメーターを保存します。

C# の場合:

public partial class AppDelegate : FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        App.ScreenWidth  = (int)UIScreen.MainScreen.Bounds.Width;
        App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
        ...
    }
}

レスポンシブ レイアウトの例

ここで、アプリケーションのレイアウトを設定する必要があります。この例では、隣接したレイアウトの幅の変更によるさまざまな幅密度への XamDataGrid コントロールの反応を制御し表示するために、いくつかのネストしたレイアウトを使用しています。

理解しやすいようにこれらのレイアウトの配置を、次の図に示します。

Responsive Layout On Neighbor Resize 1.png
  1. メインビューに次のコードを追加して、Button をクリックしたときの LeftGutter のサイズの変化に応じてサイズ変更される 1 つの XamDataGrid コントロールで上記のレイアウトを作成します。

XAML の場合:

<Grid x:Name="RootLayout" Margin="10"  >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Button x:Name="Button" Text="Change Grid Size"/>

    <Grid x:Name="ContentLayout" Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid x:Name="LeftGutter" Grid.Column="0"
               MinimumWidthRequest="0"
               MinimumHeightRequest="100"
               BackgroundColor="#FFCBCBCB" />

        <ig:XamDataGrid x:Name="DataGrid" Grid.Column="1"
                        AutoGenerateColumns="False">
            <ig:XamDataGrid.ItemsSource>
                <local:SampleSalesTeam />
            </ig:XamDataGrid.ItemsSource>
            <ig:XamDataGrid.Columns>
                <ig:TextColumn PropertyPath="FirstName" />
                <ig:TextColumn PropertyPath="Territory" />
                <ig:NumericColumn PropertyPath="Sales"  />
            </ig:XamDataGrid.Columns>
        </ig:XamDataGrid>
    </Grid>
</Grid>
  1. LeftGutter および DataGrid の幅のサイズを保存するために、int 型の 5 つの変数を作成します。

XamDataGrid コントロールは、幅の変化に応答しますが、幅は隣接したレイアウトの変更に応じて変化します。このシナリオでは、この隣接したレイアウトの 3 つの個別幅を画面全体幅に対する割合で切り替えます。この例では、0%、25%、および 50% を使用します。XamDataGrid コントロールは、応答でこれら 3 つの状態に対して、それぞれ画面の幅を残りの画面の幅の 100%、75%、および 40% に縮小します。

LeftGutterWidth25 および LeftGutterWidth50 変数は、ContentMaxWidth 変数を使用して保存された画面の最大幅の 25% および 50% のピクセル単位の値を計算して保存するために使用されます。

DataGridWidth1 および DataGridWidth2 変数は、 XamDataGrid コントロールが画面をリサイズした後に使用可能な画面幅を計算して保存するために使用されます。この値は、ResponsiveStates の範囲の値を設定するために必要です。

static int ContentMaxWidth = (int)(App.ScreenWidth / App.ScreenDensity);
static int LeftGutterWidth25 = (int)(ContentMaxWidth * .25);
static int LeftGutterWidth50 = (int)(ContentMaxWidth * .50);
static int DataGridWidth1 = ContentMaxWidth - LeftGutterWidth25;
static int DataGridWidth2 = ContentMaxWidth - LeftGutterWidth50;
  1. 次のコードを追加して、XamDataGrid コントロールの 3 つの幅の値 (100%、50%、および 25% の増分幅) を切り替えるボタンの Click イベントをハンドルします。この値は、画面で使用可能な幅に対する比率です。

C# の場合:

Button.Clicked += Button_Clicked;
...
private void Button_Clicked(object sender, EventArgs e)
{
    // Condition 1: if LeftGutter has its default width
    // then set its width to LeftGutterStop: 25% of screen width
    if (LeftGutter.Width <= 0)
    {
        LeftGutter.WidthRequest = LeftGutterWidth25;
    }
    // Condition 2: if LeftGutter's width is equal to LeftGutterWidth25
    // then set its width to LeftGutterWidth50: 50% of screen width
    else if (LeftGutter.Width == LeftGutterWidth25)
    {
        LeftGutter.WidthRequest = LeftGutterWidth50;
    }
    // Condition 3: if LeftGutter's width is equal to LeftGutterWidth50
    // then set its width to  default state of 0
    else if (LeftGutter.Width == LeftGutterWidth50)
    {
        LeftGutter.WidthRequest = 0;
    }
}
  1. 以下のコードに示すように、ResponsivePhase オブジェクトを作成するヘルパー メソッドを追加します。

C# の場合:

private ResponsivePhase CreatePhase(string columnName, string propertyName, object value)
{
    var setter = new ColumnPropertySetter()
    {
        ColumnName = columnName,
        PropertyName = propertyName,
        Value = value,
    };
    return new ResponsivePhase().AddColumnPropertySetter(setter);
}
  1. XamDataGrid コントロールの幅が調整される前のデフォルト状態で使用される最初の ResponsiveState を作成します。

このレスポンシブ状態は、XamDataGrid コントロールの幅が DataGridWidth1 から ContentMaxWidth 値の範囲内にあるときにアクティブになり、Territory 列および Sales 列を表示します。

C# の場合:

var state1 = new ResponsiveState();
state1.MaximumWidth = ContentMaxWidth;
state1.MinimumWidth = DataGridWidth1 + 1;
state1.AddResponsivePhase(CreatePhase("Territory", "IsHidden", false));
state1.AddResponsivePhase(CreatePhase("Sales", "IsHidden", false));
  1. XamDataGrid コントロールの幅を DataGridWidth1DataGridWidth2 の間に変更し、Sales 列を非表示にするときに使用される 2 つ目の ResponsiveState を作成します。

C# の場合:

var state2 = new ResponsiveState();
state2.MaximumWidth = DataGridWidth1;
state2.MinimumWidth = DataGridWidth2 + 1;
state2.AddResponsivePhase(CreatePhase("Territory", "IsHidden", false));
state2.AddResponsivePhase(CreatePhase("Sales", "IsHidden", true));
  1. XamDataGrid コントロールの幅を DataGridWidth2 より小さく変更し、Territory および Sales 列を非表示にするときに使用される 3 つめの ResponsiveState を作成します。

C# の場合:

var state3 = new ResponsiveState();
state3.MaximumWidth = DataGridWidth2;
state3.MinimumWidth = 0;
state3.AddResponsivePhase(CreatePhase("Territory", "IsHidden", true));
state3.AddResponsivePhase(CreatePhase("Sales", "IsHidden", true));
  1. 次のコード スニペットに示すように、XamDataGrid コントロールに 3 つの ResponsiveState オブジェクトをすべて追加します。

C# の場合:

DataGrid.ResponsiveStates.Add(state1);
DataGrid.ResponsiveStates.Add(state2);
DataGrid.ResponsiveStates.Add(state3);
  1. アプリケーションを保存して実行し、XamDataGrid コントロールのレスポンシブ レイアウト ビヘイビアを確認します。

レスポンシブ レイアウトのプレビュー

次のアニメーション グラフィックスは、隣接するレイアウト (LeftGutter) の幅が 3 回変化した場合の XamDataGrid コントロールのレイアウト応答を示しています。

  • 最初、LeftGutter の幅は 0 です。DataGrid は 3 つの列 (NameTerritorySales) をすべて表示します。これは最初のレスポンシブ状態を表します。

  • 次に、LeftGutter の幅が画面の幅の 25% に増加します。Sales 列を非表示にする第 2 のレスポンシブ状態がトリガーされます。

  • 再度幅を増加した後、LeftGutter は画面の幅の 50% を占め、3 番目のレスポンシブ状態で Territory 列が非表示になります。

  • 最後に、LeftGutter の幅が 0 にリセットされ、最初のレスポンシブ状態がトリガーされ、Territory および Sales の両方の列が再度表示されます。

Responsive Layout On Neighbor Resize 2.gif

レスポンシブ レイアウトのアニメーション

グリッドにアニメーションを適用し、レスポンシブ状態間のスムーズなトランジションを表示することもできます。たとえば、データ グリッド内の列非表示操作のフェードアウト アニメーションを適用すると、次のようになります。

XAML の場合:

<ig:XamDataGrid x:Name="DataGrid" ColumnHidingAnimationMode="FadeOut">
...
</ig:XamDataGrid>

C# の場合:

DataGrid.ColumnHidingAnimationMode = ColumnHidingAnimationMode.FadeOut;

このアニメーション化されたグラフィックスは、 XamDataGrid コントロールのレイアウトが列非表示操作のアニメーションにどのように反応するかを示しています。

Responsive Layout On Neighbor Resize 3.gif

関連コンテンツ

以下の表は、このトピックに関連するトピックを示します。

トピック 目的

このトピックでは、XamDataGrid コントロールの回転でレスポンシブ レイアウトを実装する方法について説明します。

このトピックでは、XamDataGrid コントロールの列および行に対し操作を実行する場合のアニメーションを適用する方法について説明します。

このトピックでは、XamDataGrid コントロールでサポートされている列タイプについて説明します。

このトピックでは、XamDataGrid コントロールの列を操作する際のコード例を紹介します。