2016-10-15 30 views
0

我现在已经使用了SemanticZoom.ZoomedInView含有ListView但我想用ItemsConrol而不是ListView由于种种原因主要其中之一是ItemsControl附带根据内部数据变量高度行,不像ListView已固定的行高。如何在UWP中的SemanticZoom.ZoomedInView中使用ItemsControl?

但我无法使用ItemsControl,因为SemanticZoom.ZoomedInView只需要ListViewBase类型的元素。那么我的其他选择是什么。

+0

我用了一个默认的'ListView'放4种不同尺寸的“矩形”作为4个项目,您可以选择e [渲染图像](https://i.stack.imgur.com/jAnD7.gif),因此“与具有固定行高的ListView不同”。那你的意思是什么? –

+0

但是我必须知道前面的行的大小正确吗?如果我需要文本内容并且每行的大小等于内容,会导致什么情况。 – AbsoluteSith

+0

我写了一个答案来解释这种行为,如果你还有问题,请留下评论。 –

回答

1

但是我必须知道手前的行大小吗?如果我需要文本内容并且每行的大小等于内容,会导致什么情况。

不,你不需要。我假设你添加文字内容各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 = "abcdefghijklmnopqrstuvwxyz"; 
    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

enter image description here

问题是TextBlock的属性被设置为默认或计算值:

enter image description here

这让所有的物品具有相同的大小,但如果添加文本内容是这样的原因是不相关的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); 
} 

然后你就可以找到区别:

enter image description here

更新:

您可以使用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; } 
} 

新的渲染图像:

enter image description here

+0

如何在使用DataTemplate使用ListView.ItemTemplate时提出这样的建议?导致它有点麻烦创建所有的DataTemplate值并手动添加它? – AbsoluteSith

+0

谢谢!作品! – AbsoluteSith

相关问题