无论我做什么,ScrollViewer都在考虑所有事情。 :(

时间:2013-07-09 19:57:07

标签: wpf xaml tabcontrol scrollviewer xamlreader

所有

我是WPF的新手,这似乎是很多人都在努力解决的问题。我把以下的XAML放在一起,然后以编程方式加载到代码中。仅使用模板并将其应用于代码实例化的TabControl对象。从那里,我为标签生成标签和随机假数据。问题是我的TabControl的ScrollViewer无限增长,因此不会滚动。我试图将树上的所有VerticalAlignment设置为“Stretch”,但它没有帮助。我觉得这是一个简单的问题,它是通过拼凑多个自定义并在运行时将它们全部实例化而产生的。任何帮助将不胜感激。

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    Title="Window2">
<TabControl>
    <TabControl.Template>
        <ControlTemplate TargetType="TabControl">
            <DockPanel VerticalAlignment="Stretch">
                <ScrollViewer VerticalAlignment="Stretch">
                    <ScrollViewer.Template>
                        <ControlTemplate TargetType="{x:Type ScrollViewer}">
                            <Grid x:Name="Grid" VerticalAlignment="Stretch" Background="{TemplateBinding Background}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Rectangle x:Name="Corner" Grid.Column="0" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Row="1"/>
                                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" Margin="{TemplateBinding Padding}" Grid.Row="0"/>
                                <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="0" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                <ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                            </Grid>
                        </ControlTemplate>
                    </ScrollViewer.Template>
                    <TabPanel x:Name="HeaderPanel"
                          Panel.ZIndex ="1" 
                          KeyboardNavigation.TabIndex="1"
                          Grid.Column="0"
                          Grid.Row="0"
                          Margin="2,2,2,0"
                          IsItemsHost="true"/>
                </ScrollViewer>
                <ContentPresenter x:Name="PART_SelectedContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Padding}" ContentSource="SelectedContent"/>
            </DockPanel>
        </ControlTemplate>
    </TabControl.Template>
</TabControl>

TabControl tabs = new TabControl();
        string xamlPath = System.IO.File.ReadAllText("..\\..\\Resources\\Templates\\Template.xaml");
        Window window = (Window)XamlReader.Parse(xamlPath);
        tabs.Template = ((TabControl)window.Content).Template;

        Random rand = new Random();

        for (int i = 0; i < 100; i++)
        {

            DataTable dataTable = new DataTable();
            for (int x = 0; x < 50; x++) dataTable.Columns.Add(x.ToString(), typeof(double));

            for (int x = 0; x < 50; x++)
            {
                DataRow dataRow = dataTable.NewRow();
                dataTable.Rows.Add(dataRow);
            }
            DataView dataView = new DataView(dataTable);
            for (int x = 0; x < 50; x++)
            {
                for (int y = 0; y < 50; y++) dataView[x][y] = rand.Next()%100;
            }

            DataGrid table = new DataGrid();
            table.ItemsSource = dataView;


            TabItem tab = new TabItem();
            tab.Header = "Tab " + (i+1);
            tab.Content = table;

            tabs.Items.Add(tab);
        }
        tabs.TabStripPlacement = Dock.Left;
        tabs.VerticalAlignment = VerticalAlignment.Stretch;
        tabs.HorizontalAlignment = HorizontalAlignment.Stretch;
        option.canvas.Children.Add(tabs);

2 个答案:

答案 0 :(得分:0)

需要约束TabControl

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    Title="Window2">
    <Grid ShowGridLines="False">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TabControl Grid.Row="0" Grid.Comlumn="0"/>
    </Grid>
</Window>

答案 1 :(得分:0)

编辑:以下是我原来的解决方案。事实证明问题是首先使用Canvas。通过使用DockPanel,它完全不需要重新计算尺寸。这与Blam的答案类似。

强制画布具有高度/宽度就可以了。它拒绝计算布局渲染前的高度,因为所有内容都是动态调整大小的,因此Content对象内的顶级Panel显然必须具有手动计算的大小。当然,这就是XAML / C#屏障所在,它可以很好地解决问题。

以下代码是缺少的。在我添加它之后,原始帖子中的XAML和C#代码完全符合要求。基本上,顶级面板必须具有较低级别对象的大小,以便围绕它动态调整大小。

option.canvas.Height = OptionTabs.ActualHeight - tab.ActualHeight - tab.Margin.Bottom - tab.Margin.Top;
option.canvas.Width = OptionTabs.ActualHeight - tab.Margin.Right - tab.Margin.Left;