wpf列表框在包含网格调整大小时重置selectedIndex

时间:2014-01-13 14:51:08

标签: wpf listbox selectedindex

我需要一个复合视图,其中包含一个可隐藏的顶部,一个由左侧导航菜单(列表框)组成的底部和一个右侧的内容区域。

列表框中的一个选项应该使顶部部分折叠,其他列表选项应该显示顶部部分。

我的想法是使用具有三行的网格(用于网格划分器的中间行)在顶部和底部分割屏幕,并且当前使用代码隐藏来改变顶部网格的高度。

问题是当我从codebehind设置Grid.Height-prop时,我的ListBox.SelectedIndex被设置为1到0!事情是,如果我改变两个菜单项的顺序,它工作正常!

我们正在使用Caliburn Micro,但我认为这不会改变任何内容 - 该示例也可以使用INotifyPropertyChanged实现。

<UserControl x:Uid="UserControl_1" x:Class="VisionAir.Client.Gui.Common.Windows.Foo.Views.BarView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" Height="300" Width="300">
    <UserControl.Resources>
        <ResourceDictionary>
            <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid Name="ResizableGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="100" Name="TopRow"/>
            <RowDefinition Height="10" Name="SplitterRow"/>
            <RowDefinition Name="BottomRow"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Name="TopRowGrid" Background="Chartreuse" Visibility="{Binding ShowSearchView, Converter={StaticResource BoolToVisConverter}}"/>
        <GridSplitter Grid.Row="1" Visibility="{Binding ShowSearchView,Converter={StaticResource BoolToVisConverter}}" ResizeBehavior="PreviousAndCurrent" HorizontalAlignment="Stretch" Height="10"/>
        <Grid Name="ListGrid" Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <ListBox Grid.Column="0" ItemsSource="{Binding Items}" SelectedIndex="{Binding SelectedIndex}" DisplayMemberPath="DisplayName">
            </ListBox>

            <Grid Background="Chocolate" Grid.Column="1"/>
        </Grid>
        <Canvas Name="FooCanvas" Visibility="{Binding ShowSearchView, Converter={StaticResource BoolToVisConverter}}"></Canvas>
    </Grid>
</UserControl>


using System.Windows;
using System.Windows.Controls;

namespace VisionAir.Client.Gui.Common.Windows.Foo.Views
{
    /// <summary>
    /// Interaction logic for BarView.xaml
    /// </summary>
    public partial class BarView : UserControl
    {
        public BarView()
        {
            InitializeComponent();

            FooCanvas.IsVisibleChanged += FooCanvas_IsVisibleChanged;
        }

        void FooCanvas_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            bool isVisible = (bool)e.NewValue;

            if (isVisible)
            {
                TopRow.Height = new GridLength(100);
                SplitterRow.Height = new GridLength(0, GridUnitType.Auto);
            }
            else
            {
                TopRow.Height = new GridLength(0, GridUnitType.Auto);
                SplitterRow.Height = new GridLength(0);
            }
        }
    }
}


using System.Collections.Generic;
using Caliburn.Micro;

namespace VisionAir.Client.Gui.Common.Windows.Foo.ViewModels
{
    public class BarViewModel : PropertyChangedBase
    {
        private List<Screen> _items = new List<Screen>();

        public BarViewModel()
        {
            Items.Add(new Screen() { DisplayName = "Hide" });
            Items.Add(new Screen() { DisplayName = "Show" });
        }
        public bool ShowSearchView { get { return SelectedIndex == 1; } }

        private int _selectedIndex;

        public List<Screen> Items
        {
            get { return _items; }
            set { _items = value; }
        }

        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                System.Diagnostics.Debug.WriteLine(value);

                _selectedIndex = value;

                NotifyOfPropertyChange(() => ShowSearchView);
            }
        }
    }
}

0 个答案:

没有答案