【发布时间】:2014-11-05 20:42:12
【问题描述】:
我为 ListBox 定义了样式以显示具有垂直或水平滚动方向的项目:
<Style x:Key="ListBoxVerticalStyle" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxHorizontalStyle" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
在 xaml 中静态使用时它们工作正常,例如
<ListBox Style="{StaticResource ListBoxHorizontalStyle}" ...
我正在尝试使用以下代码在 C# 中动态更新方向:
if (horizontal)
{
MyListBox.Style = Resources["ListBoxHorizontalStyle"] as Style;
}
else
{
MyListBox.Style = Resources["ListBoxVerticalStyle"] as Style;
}
MyListBox.InvalidateMeasure();
MyListBox.InvalidateArrange();
ListBox.ScrollViewer 的方向确实发生了变化,但项目仍以原始方向堆叠。好像ItemsPanel 更新没有得到应用。我需要做些什么来强制 ListBox 完全刷新自己吗?
【问题讨论】:
标签: silverlight windows-phone-8 listbox orientation itemspanel