現状のXamarin.Forms.WPFのListViewのViewCellは、ウインドウ幅を変えても、大きさが変わりません。そこで、カスタムレンダラーで、ちゃんとウインドウ幅に追従するようにしてみました。
実装
.NET Standard
namespace CustomListView.Controls
{
public class CustomListView : ListView
{
public CustomListView() : base()
{
}
public CustomListView(ListViewCachingStrategy cachingStrategy) : base(cachingStrategy)
{
}
}
}
WPF
[assembly: ExportRenderer(typeof(CustomListView.Controls.CustomListView), typeof(CustomListView.WPF.Renderers.CustomListViewRenderer))]
namespace CustomListView.WPF.Renderers
{
public class CustomListViewRenderer : ListViewRenderer
{
protected override void UpdateWidth()
{
base.UpdateWidth();
if (Control != null && Control.ItemsSource != null)
{
foreach (var item in Control.ItemsSource)
{
if (item is ViewCell viewCell)
{
var element = Platform.GetRenderer(viewCell.View)?.GetNativeElement();
if (element != null)
{
element.Width = Control.Width - 36;
}
}
}
}
}
}
}
ウインドウ幅が変わった時に、UpdateWidth
が呼ばれるので、そこで、全ViewCell
の幅を変更するようにしています。
結果
通常
CustomListView
ソース
全体のソースはこちらです。