选项卡中的Listview在创建水平滚动条之前会延伸到窗口边框之外

时间:2015-06-14 17:48:24

标签: c# wpf xaml

我做了一个简单的程序来演示正在发生的事情。

UserControl1包含一个包含一些简单信息的列表视图。 GridViewcolumns'此列表视图中的宽度设置为自动以允许重新调整大小。

UserControl2是一个TabControl,其中包含一个包含UserControl1的选项卡。

MainWindow有一个网格,左侧为UserControl1,右侧为UserControl2。

我在主窗口的左侧看到了正确的行为,如果我通过右边框重新调整Publisher列的大小,则会出现一个水平滚动条。

在右侧,如果我执行相同的操作,TabControl似乎会在水平滚动条出现之前延伸超过窗口的右侧。

有人可以解释为什么会这样吗?我期望UserControl1在Tab中以与在主网格中相同的方式操作。为什么它会越过窗口边界?

代码

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*" MinWidth="300"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="6*" MinWidth="300"/>
        </Grid.ColumnDefinitions>
        <controls:UserControl1 x:Name="localPanel" Margin="2" HorizontalContentAlignment="Stretch" Grid.Column="0"/>
        <GridSplitter Grid.Column="1" Grid.Row="3" Width="2" HorizontalAlignment="Stretch" ResizeDirection="Columns"/>
        <controls:UserControl2 x:Name="tabPanel" Margin="2" Grid.Column="2"/>
    </Grid>
</Window>

UserControl1.xaml

<UserControl x:Class="WpfApplication1.UserControl1"
                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"
                DataContext="{Binding RelativeSource={RelativeSource Self}}"
                mc:Ignorable="d" 
                d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="2" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ListView Grid.Row="2" ItemsSource="{Binding GameCollection}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="auto" Header="Game Name" 
                DisplayMemberBinding="{Binding GameName}"  />
                    <GridViewColumn Width="auto" Header="Creator"  
                DisplayMemberBinding="{Binding Creator}" />
                    <GridViewColumn Width="auto" Header="Publisher" 
                DisplayMemberBinding="{Binding Publisher}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</UserControl>

UserControl1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        ObservableCollection<GameData> _GameCollection =
        new ObservableCollection<GameData>();

        public UserControl1()
        {
            _GameCollection.Add(new GameData
            {
                GameName = "World Of Warcraft",
                Creator = "Blizzard",
                Publisher = "Blizzard"
            });
            _GameCollection.Add(new GameData
            {
                GameName = "Halo",
                Creator = "Bungie",
                Publisher = "Microsoft"
            });
            _GameCollection.Add(new GameData
            {
                GameName = "Gears Of War",
                Creator = "Epic",
                Publisher = "Microsoft"
            });

            InitializeComponent();
        }

        public ObservableCollection<GameData> GameCollection
        { get { return _GameCollection; } }

        private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ListView _ListView = sender as ListView;
            GridView _GridView = _ListView.View as GridView;
            var _ActualWidth = _ListView.ActualWidth - SystemParameters.VerticalScrollBarWidth;
            for (Int32 i = 1; i < _GridView.Columns.Count; i++)
            {
                _ActualWidth = _ActualWidth - _GridView.Columns[i].ActualWidth;
            }
            _GridView.Columns[0].Width = _ActualWidth;
        }

        public class GameData
        {
            public string GameName { get; set; }
            public string Creator { get; set; }
            public string Publisher { get; set; }
        }
    }
}

UserControl2.xaml

<UserControl x:Class="WpfApplication1.UserControl2"
             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"
             xmlns:controls="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TabControl>
            <TabItem Header="Profile Setup">
                <Grid>
                    <controls:UserControl1/>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</UserControl>

0 个答案:

没有答案