Silverlight 使用样式动态更改列表框方向
我为列表框定义了样式,以显示垂直或水平滚动方向的项目:Silverlight 使用样式动态更改列表框方向,silverlight,windows-phone-8,listbox,orientation,itemspanel,Silverlight,Windows Phone 8,Listbox,Orientation,Itemspanel,我为列表框定义了样式,以显示垂直或水平滚动方向的项目: <Style x:Key="ListBoxVerticalStyle" TargetType="ListBox"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> <
<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>
ListBox.ScrollViewer
的方向确实发生了变化,但项目仍按其原始方向堆叠。好像没有应用ItemsPanel
更新。是否需要执行某些操作来强制列表框完全刷新自身?检查此链接:
该教程解释了如何处理屏幕方向。我不认为这样做会引发PropertyChange事件。在我看来,我只有两个解决方案。一个是从您自己的自定义列表框和VisualStates派生出来的,在这里作为解决方案还远远不够。另一个选项非常简单,我们只需要通知属性已更改,我知道的最简单的方法就是将其绑定到ViewModel 为此,我将使用页面作为ViewModel,因此您的XAML如下
这个链接与我的问题无关。请不要混淆列表框方向和屏幕方向。谢谢,我在视图模型中添加了一个方向属性,它可以正常工作。我以为我已经想出了一些方便的风格,但我想这是行不通的。
<ListBox Style="{StaticResource ListBoxHorizontalStyle}" ...
if (horizontal)
{
MyListBox.Style = Resources["ListBoxHorizontalStyle"] as Style;
}
else
{
MyListBox.Style = Resources["ListBoxVerticalStyle"] as Style;
}
MyListBox.InvalidateMeasure();
MyListBox.InvalidateArrange();
<ListBox x:Name="myListBox">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="{Binding MYO}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
using System.ComponentModel; // INotifyPropertyChanged
public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
// implement the INotify
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
// Constructor
public MainPage()
{
InitializeComponent();
}
private System.Windows.Controls.Orientation _myo;
public System.Windows.Controls.Orientation MYO
{
get { return _myo; }
set { _myo = value; NotifyPropertyChanged("MYO"); }
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myListBox.DataContext = this; // have to set the datacontext to point to the page
MYO = System.Windows.Controls.Orientation.Horizontal; // set it to Horizontal
// MYO = System.Windows.Controls.Orientation.Vertical; // set it to Vertical
}
}