2010-12-20 41 views
0

我想添加缩放功能到数据绑定列表框。什么是最有效的方法来做到这一点?我已将ListBox放置在一个Grid控件中并使其可滚动。捏手放在Windows Phone 7的列表框上缩放

这是我现在的代码。

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,0,10,10" Background="Black" > 
     <ListBox Name="lstText" FontSize="24" Foreground="White" SelectionMode="Single" Margin="10,0,10,10" ScrollViewer.VerticalScrollBarVisibility="Visible" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel > 
         <TextBlock Text="{Binding Text}" TextWrapping="Wrap"></TextBlock>       
        </StackPanel>       
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
    <toolkit:GestureService.GestureListener> 
     <toolkit:GestureListener 
      Tap="GestureListener_Tap" 
      PinchCompleted="GestureListener_PinchCompleted" 
      Flick="GestureListener_Flick"> 

     </toolkit:GestureListener> 
    </toolkit:GestureService.GestureListener> 

回答

1

该列表框的设计不是缩放(通过捏或任何其他方法)。

如果你想实现这个,你必须重画不同缩放级别的内容。
你就会有一个数量的问题虽然解决:

  • 你怎么通知他们可以改变文字大小这样的用户?
  • 如何避免影响滚动和选择列表框中项目的标准行为。
  • 滚动应如何处理包装和当前显示的文本?
  • 列表不应该用于在手机上显示大量文本。如果您需要显示大量文本,请在列表中标注一个简短的“标题”,然后在另一个页面中显示详细信息。这样,列表中的文本总是可以以足够大的方式显示,以至于永远不需要更改,并且应该始终可读。
  • 这是一个真正的问题,你正试图克服或只是你认为会很好的东西?手机不会仅用于您的应用程序,那么如果用户仍然必须在操作系统和其他应用程序中使用具有固定文本大小的列表,那么为什么还需要这种手机。
  • 当您更改文本的大小时,框架会重新绘制列表中的所有内容,您会遇到潜在的性能问题。您可以查看使用延迟加载,只需重新绘制缩放时在屏幕上显示的内容,但这会影响您如何确定尺寸更改时显示内容的顶部(和底部)。

摘要:这几乎肯定是不必要的,而且会很复杂,很难做好。如果你真的想尝试这个有一个去,然后发布任何问题的代码。

+0

感谢您的回复。按我所说的文本缩放,它应该增加和减少ListBox内的字体大小。那么你能指点我一个代码示例吗? – Daniel 2010-12-20 16:25:00

+0

我在这个列表框中显示来自圣经的经文,但是我想保持滚动功能,以便用户可以滚动选定章节的经文。不知道这是否澄清了这个概念。 – Daniel 2010-12-20 17:34:24

+0

@Daniel你不需要一个Listbox就可以滚动。只需将文本包装在ScrollViewer中即可。 – 2010-12-20 17:46:47

0

Alex Yakhnin提供了滚动长文本的解决方案。

Creating Scrollable TextBlock for WP7. - Alex Yakhnin's Blog

你可以用一个ScrollViewer中一个TextBlock这可能足以满足您的需求。如果你的文字够长,随着文字变得更大,你会碰到各种各样的墙壁。 Alex的解决方案是一个控件,它将一个StackPanel包装在一个ScrollViewer中,并将TextBlocks添加到StackPanel的可管理部分中。

+0

我正在寻找缩放文本控件的解决方案。不管它的列表框或文本块如何。但是我想使用ListBox,因为我有多行数据要显示。我希望这说得很清楚。 – Daniel 2010-12-21 03:08:07

+0

当然,你可以使用列表框来滚动文本框,但是有一个问题。 Listbox不会使用可变高度的listboxitems来显示虚拟化。我很好奇你附加了什么功能的列表框,让你在显示文本上设置这个控件。我也很好奇你想要捏和放大为你做什么。 – 2010-12-21 08:36:55

+0

我使用ListBox来显示数据库中的多行。所以用户可以与每个单独的项目交互。 – Daniel 2010-12-21 14:14:31

0

我已经在ManipulationDelta这样做自己,但它并不平坦所有

在类属性

x:local="clr-namespace:YourApplicationNamespace" 

在XAML:

<Grid x:Name="LayoutRoot" ManipulationDelta="LayoutRoot_ManipulationDelta"> 
    <Grid.Resources> 
     <local:CustomSettings x:Key="Settings"/> 
     <DataTemplate x:Key="verseDataTemplate"> 
      <TextBlock FontSize="{Binding Path=Font35, Source={StaticResource Settings}}" 
        Text="{Binding}"/> 
     </DataTemplate> 
    </Grid.Resources> 
    <ListBox ItemTemplate="{StaticResource verseDataTemplate}"/> 
在后面的代码

private void LayoutRoot_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) 
    { 
     try 
     { 
      var fnt = lboVerses.FontSize; 
      if (e.DeltaManipulation.Scale.X == 0 || e.DeltaManipulation.Scale.Y == 0) return; 
      if (e.DeltaManipulation.Scale.X > 1 || e.DeltaManipulation.Scale.Y > 1) 
      { 
       if (fnt < 72) 
        BibliaSettings.font35++; 
      } 
      else if (e.DeltaManipulation.Scale.X < 1 || e.DeltaManipulation.Scale.Y < 1) 
      { 
       if (fnt > 5) 
        BibliaSettings.font35--; 
      } 
     } 
     catch (Exception x) 
     { 
      Debugger.Log(0, "Errors", x.Message + "\n" + x.StackTrace); 
     } 
    } 

您的CustomSettings类

public class CustomSettings : INotifyPropertyChanged 
{ 
    public static List<CustomSettings> Instances; 
    public CustomSettings() 
    { 
     if (Instances == null) Instances = new List<CustomSettings>(); 
     Instances.Add(this); 
    } 
    public static int font35 
    { 
     get 
     { 
      return Get("Font35", 35); //Provide mechanism to get settings 
     } 
     set 
     { 
      Save(value, "Font35");//Provide mechanism to store settings 
      Instances.ForEach(inst => inst.OnPropertyChanged("Font35")); 
     } 
    } 
    public int Font35 
    { 
     get 
     { 
      return font35; 
     } 
     set 
     { 
      font35=value; 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
}