【问题标题】:Dynamically change ListBox orientation with Style使用 Style 动态更改 ListBox 方向
【发布时间】: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


    【解决方案1】:

    我认为这样做不会引发 PropertyChange 事件。在我脑海中,我只有两个解决方案。一个是派生出您自己的自定义 ListBox 和 VisualStates,作为解决方案在这里放置的时间太长了。另一个选项非常简单,我们只需要通知属性已更改,我知道如何做到这一点的最简单方法就是将其绑定到您的 ViewModel。

    为此,我只是将Page用作ViewModel,所以你的XAML就是这样


    <ListBox x:Name="myListBox">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="{Binding MYO}"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
    

    C#

    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
        }
    }
    

    【讨论】:

    • 谢谢,我在视图模型中添加了一个方向属性,它可以工作。我以为我想出了一些方便的样式,但我想它不会成功。
    【解决方案2】:

    查看此链接:http://msdn.microsoft.com/en-us/library/windows/apps/jj207002(v=vs.105).aspx

    该教程解释了如何处理屏幕方向。

    【讨论】:

    • 该链接与我的问题无关。请不要将 ListBox 方向与屏幕方向混淆。
    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多