【发布时间】:2012-05-19 18:12:29
【问题描述】:
我对 ListBox 及其元素有一点问题。正如您在我的代码中看到的那样,两个文本块的堆栈面板都设置为 250。这样设置是因为否则文本会在 1 行中展开并且您看不到按钮。显而易见的问题是,将此参数设置为静态,如果分辨率或方向更改不会完全适合屏幕。我想知道是否有办法动态设置这个宽度。
<ListBox x:Name="attractionsListbox" Margin="0,0,-12,0" ItemsSource="{Binding Attractions}" Loaded="attractionsListbox_Loaded">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Tag="{Binding Name}" Tap="AttractionListBoxItem_Tap">
<Image Height="140" Width="140" Margin="0,5,0,0" Source="{Binding Thumbnail}" MaxWidth="140" MaxHeight="140" Stretch="Fill" />
<StackPanel Width="250">
<TextBlock x:Name="AttractionName" Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSmallStyle}" FontSize="20" Foreground="Black" FontWeight="Bold" />
<TextBlock Text="{Binding ShortDescription}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSmallStyle }" Foreground="Black" />
</StackPanel>
<Button Width="80" Height="80" Padding="10,3,10,5" BorderBrush="Black" Click="Add_Todo_Click" Tag="{Binding Name}">
<Button.Background>
<ImageBrush ImageSource="/SundsvallWP7;component/Images/appbar.add.rest.png"/>
</Button.Background>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
(android中有权重,可以给每个控件分配权重,不知道Windows Phone有没有这个)。
非常感谢。
【问题讨论】:
-
你为什么不在列表框上使用 SelectionChanged?
-
据我所知,如果我使用它,当我点击按钮时它会触发该事件,这不是我想要的,因为按钮有它自己的特定功能。
-
该按钮将吸收该事件 - 似乎您的设置与 SelectionChanged 几乎相同,b/c 您在具有 Tap 事件的容器内有一个按钮。
-
没错,刚刚意识到。试图解决我改变的主要问题。我添加了一个额外的水平方向的堆栈面板,并使用 Tap 事件将按钮从堆栈面板中取出。这样它就可以正常工作了。
-
解决您的问题的方法是创建一个具有 3 个列定义的网格:自动、*(填充)和自动。将图像放在 Grid.Column="0" 中,将 StackPanel 放在 Grid.Column="1" 中(也删除宽度),将 Button 放在 Grid.Column="2" 中。
标签: windows-phone-7 xaml stackpanel