如何测量未选中的选项卡?

时间:2014-03-21 20:57:58

标签: c# wpf tabcontrol virtualization

简明扼要的问题:
是否可以测量未选择Tab的内容?

问题摘要:
想象一下,你有一个带有两个标签的TabControl。第一个选项卡包含一个带有TextBlock的Grid。您选择第二个选项卡。经过一段时间后,TextBlock的Text字段变为非常长的字符串。您希望在可见之前测量第一个标签内容的大小。

如果您只是执行一个度量,它将无法捕获字符串已更改的事实 - 您可以在WPF Visualizer中看到TextBlock在其Text字段中具有新字符串,但TextBlock拒绝重新测量。如果直接测量字符串,可以检测新的所需大小,但这不是一个好的解决方案;我希望能够衡量第一个标签的总内容,而不仅仅是字符串。

无论如何,为长示例代码道歉,这很难进一步减少。当窗口出现时,代码等待两秒钟,然后交换选项卡。然后它改变字符串。测量循环在检测到字符串大小更改时将背景更改为蓝色,并在检测到实际内容大小更改时将其更改为红色。红色仅在切换标签时发生,但我希望能够在不必切换回第一个标签的情况下发生红色。

代码背后:

    public MainWindow()
    {
        InitializeComponent();

        // this worker waits a bit so the first tab renders, 
        // then it switches tabs and changes the string.
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += delegate
        {
            Thread.Sleep(2000);
            Application.Current.Dispatcher.BeginInvoke(new Action(delegate
            {
                TestTabControl.SelectedIndex = 1;
                TestBlock.Text = "This is a long string that ought to change the measure of the textblock to sizes never before seen by human eyes";
                this.Background = Brushes.Green;
            }));
        };
        worker.RunWorkerAsync();

        // This worker constantly measures the text block 
        worker = new BackgroundWorker();
        worker.DoWork += delegate
        {
            while (true)
            {
                if (Application.Current != null)
                {
                    Application.Current.Dispatcher.BeginInvoke(new Action(delegate
                    {
                        MeasureFirstTab();
                    }));
                }
                else
                {
                    break;
                }
                Thread.Sleep(100);
            }
        };
        worker.RunWorkerAsync();
    }

    // Turn the background red when the tab width changes
    public void MeasureFirstTab()
    {
        FirstTabContent.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        if (MeasureString(TestBlock.Text).Width > 500)
        {
            this.Background = Brushes.Blue;
        }

        if (FirstTabContent.DesiredSize.Width > 500)
        {
            this.Background = Brushes.Red;
        }  
    }

    private Size MeasureString(string candidate)
    {
        var formattedText = new FormattedText(
            candidate,
            CultureInfo.CurrentUICulture,
            FlowDirection.LeftToRight,
            new Typeface(TestBlock.FontFamily, TestBlock.FontStyle, TestBlock.FontWeight, TestBlock.FontStretch),
            TestBlock.FontSize,
            Brushes.Black);

        return new Size(formattedText.Width, formattedText.Height);
    }
}

XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TabControl Name="TestTabControl">

        <TabItem Header="Changes">
            <Grid Name="FirstTabContent">
                <TextBlock Name="TestBlock" Text="small"/>
            </Grid>
        </TabItem>
        <TabItem Header="Short">                
            <TextBlock Text="Short"/>                
        </TabItem>
    </TabControl>        
</Grid>

1 个答案:

答案 0 :(得分:0)

回答后人以及将来搜集这个问题的任何人。

正如我对OP的评论所述,TabControl虚拟化了未选中的标签,这显然导致测量通过仅计算之前的所需大小。

因此,解决方案是从选项卡项中删除内容,将其添加到网格,测量内容,然后将其添加回选项卡。

以下是新的衡量标准代码:

    // Turn the background red when the tab width changes
    public void MeasureFirstTab()
    {
        // Remember the previous selected item
        object selectedItem = TestTabControl.SelectedItem;
        Grid measureBox = new Grid();
        UIElement content;

        // Iterate through all items
        foreach (TabItem obj in TestTabControl.Items)
        {
            // Get the tab content into the grid
            TestTabControl.SelectedItem = obj;
            content = (UIElement)obj.Content;
            obj.Content = null;
            measureBox.Children.Add(content);

            // Measure the content
            content.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

            if (content.DesiredSize.Width > 500)
            {
                this.Background = Brushes.Red;
            }

            // Return the content to its rightful owner
            measureBox.Children.Clear();
            obj.Content = content;
        }

        // Reset the tab control
        TestTabControl.SelectedItem = selectedItem;

    }