如何在 UWP SemanticZoom.ZoomedInView 中使用 ItemsControl?
How to use ItemsControl inside SemanticZoom.ZoomedInView in UWP?
我目前使用的 SemanticZoom.ZoomedInView
包含 ListView
但我想使用 ItemsConrol
而不是 ListView
出于各种原因主要是 ItemsControl
随行的高度变化,具体取决于内部数据,这与具有固定行高的 ListView
不同。
但我无法使用 ItemsControl
,因为 SemanticZoom.ZoomedInView
仅采用 ListViewBase
类型元素。那么我的其他选择是什么。
But I have to know the size of rows before hand right? Cause what if I need text content and the size of each row be equal to content.
不,您不必这样做。我假设您像这样为每个 ListViewItem
添加文本内容:
<ListView x:Name="listView" />
<Button VerticalAlignment="Bottom" Margin="0,5" Content="Add Text Item to ListView" Click="Button_Click" />
隐藏代码:
private static string RandomString(int Size)
{
string input = "abcdefghijklmnopqrstuvwxyz0123456789";
var chars = Enumerable.Range(0, Size)
.Select(x => input[new Random().Next(0, input.Length)]);
return new string(chars.ToArray());
}
private void Button_Click(object sender, RoutedEventArgs e)
{
listView.Items.Add(RandomString(new Random().Next(1, 1000)));
}
然后它会生成一个默认的TextBlock
来包含Text
:
问题是 TextBlock
的属性设置为默认值或计算值:
这使得所有的项目都具有相同的大小,但原因与ListView
控件无关,如果您添加这样的文本内容:
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBlock tb = new TextBlock();
tb.Text = RandomString(new Random().Next(1, 500));
tb.TextWrapping = TextWrapping.Wrap;
listView.Items.Add(tb);
}
然后你就可以找到不同之处:
更新:
您可以这样使用 DataTemplate
:
<ListView x:Name="listView" ItemsSource="{x:Bind Collection}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding ItemText}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
隐藏代码:
private ObservableCollection<Model> Collection = new ObservableCollection<Model>();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
for (int i = 0; i < 50; i++)
{
Collection.Add(new Model { ItemText = RandomString(new Random().Next(100, 500)) });
}
}
public class Model
{
public string ItemText { get; set; }
}
新渲染图:
我目前使用的 SemanticZoom.ZoomedInView
包含 ListView
但我想使用 ItemsConrol
而不是 ListView
出于各种原因主要是 ItemsControl
随行的高度变化,具体取决于内部数据,这与具有固定行高的 ListView
不同。
但我无法使用 ItemsControl
,因为 SemanticZoom.ZoomedInView
仅采用 ListViewBase
类型元素。那么我的其他选择是什么。
But I have to know the size of rows before hand right? Cause what if I need text content and the size of each row be equal to content.
不,您不必这样做。我假设您像这样为每个 ListViewItem
添加文本内容:
<ListView x:Name="listView" />
<Button VerticalAlignment="Bottom" Margin="0,5" Content="Add Text Item to ListView" Click="Button_Click" />
隐藏代码:
private static string RandomString(int Size)
{
string input = "abcdefghijklmnopqrstuvwxyz0123456789";
var chars = Enumerable.Range(0, Size)
.Select(x => input[new Random().Next(0, input.Length)]);
return new string(chars.ToArray());
}
private void Button_Click(object sender, RoutedEventArgs e)
{
listView.Items.Add(RandomString(new Random().Next(1, 1000)));
}
然后它会生成一个默认的TextBlock
来包含Text
:
问题是 TextBlock
的属性设置为默认值或计算值:
这使得所有的项目都具有相同的大小,但原因与ListView
控件无关,如果您添加这样的文本内容:
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBlock tb = new TextBlock();
tb.Text = RandomString(new Random().Next(1, 500));
tb.TextWrapping = TextWrapping.Wrap;
listView.Items.Add(tb);
}
然后你就可以找到不同之处:
更新:
您可以这样使用 DataTemplate
:
<ListView x:Name="listView" ItemsSource="{x:Bind Collection}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding ItemText}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
隐藏代码:
private ObservableCollection<Model> Collection = new ObservableCollection<Model>();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
for (int i = 0; i < 50; i++)
{
Collection.Add(new Model { ItemText = RandomString(new Random().Next(100, 500)) });
}
}
public class Model
{
public string ItemText { get; set; }
}
新渲染图: