using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace TestUwp
{
public class DataItem : INotifyPropertyChanged
{
public DataItem(int id, string firstName, string lastName)
{
Id = id;
FirstName = firstName;
LastName = lastName;
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
TestData = new ObservableCollection<DataItem> {
new DataItem(1, "Pauly", "Thurlborn"),
new DataItem(2, "Orbadiah", "Ewen"),
new DataItem(3, "Britni" ,"Smead"),
new DataItem(4, "Fionna" ,"Jennemann"),
new DataItem(5, "Ashley" ,"Stoddart"),
new DataItem(6, "Bradford", "Kaesmakers"),
new DataItem(7, "Maxy" ,"Lemon"),
new DataItem(8, "Rasia" ,"Comber"),
new DataItem(9, "Colas" ,"Shepton"),
new DataItem(10, "Cacilie" ,"Tummons"),
};
DataContext = this;
}
public ObservableCollection<DataItem> TestData { get; set; }
private List<ColumnDefinition> _columns = new List<ColumnDefinition>();
private double _colSize = 0.0;
private void LabelSizeChanged(object sender, SizeChangedEventArgs e)
{
var item = (dynamic)sender;
var grid = (Grid)item.Parent;
var column = grid.ColumnDefinitions[0];
if (!_columns.Contains(column))
{
_columns.Add(column);
}
var adjustments = new List<ColumnDefinition>();
if (item.ActualWidth > _colSize)
{
_colSize = item.ActualWidth;
adjustments.AddRange(_columns);
}
else
{
adjustments.Add(column);
}
foreach (var col in adjustments)
{
col.Width = new GridLength(_colSize);
}
}
}
}
List<test> li = new List<test>();
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
for (int i = 0; i < 10; i++)
{
li.Add(new test()
{
Name1 = "Anobik1" + i.ToString(),
Name2 = "Anobik1" +i.ToString(),
Name3 = "Anobik1" +i.ToString()
});
}
MyList.ItemsSource = li;
}
型 我绑定的类如下
class test
{
public string Name1 { get; set; }
public string Name2 { get; set; }
public string Name3 { get; set; }
}
3条答案
按热度按时间fykwrbwg1#
SharedSizeGroup
是WPF专用的,在UWP中不存在。目标:创建SharedSizeGroup的替代
为了知道度量,我们需要查看所有控件并找到最大值。
我们将创造
我们使用Fody和PropertyChanged.Fody nuget包。虽然它们不是只读示例所必需的,但如果你开始修改数据,它会很有用。我添加了
SizeChanged
事件处理程序并直接修改宽度。你可以设置一个属性并绑定到,或者创建一个行为,但这是如何完成的。的数据
查看
字符串
Model和ViewModel
型
这个idea的源代码来自Xamarin。
mqkwyuun2#
我为winui做了一个模拟,参见repo
下面是如何使用,很像在WPF中
字符串
7gyucuyw3#
我认为this stackoverflow question中给出的解决方案应该可以满足您的要求,您只需将第一列更改为
Auto
而不是*
来自帖子:
字符串
和后面的代码。只是为了给予一个尝试没有使用MVVM
这是cs
型
我绑定的类如下
型