문제

가장 큰 탭 항목의 크기를 tabcontrol 할 수있는 방법이 있습니까 (실제로 Tabitem의 컨텐츠)?

TabControl에는 특정 크기가 할당되지 않으므로자가 크기가 할당되어야합니다. 올바르게 수행하지만 탭을 전환하면 자동으로 현재 선택된 탭의 내용의 높이 (및 너비)로 자체적으로 크게됩니다.

나는 크기 조정이 일어나기를 원하지 않으며, TabControl이 가장 큰 품목의 높이를 가정하게하지만 여전히 자체적으로 자체적으로 크기를 갖도록합니다.

단서가 있습니까? 나는 데이터를 시도했다 Height 멀티 핀딩 사용의 내용으로 사용되는 각 요소의 속성은 두 가지 모두에 바인딩이 있습니다. ActualHeight 그리고 Items TabControl의 속성. 그러나 아아, ActualHeight 컨텐츠 요소 중 항상 0입니다.

        <TabItem Header="Core" > 
            <Grid Margin="5">
                <Grid.Height>
                    <MultiBinding Converter="{Converters1:AllTabContentEqualHeightConverter}">
                        <Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}"/>
                        <Binding Path="Items" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}"/>
                    </MultiBinding>
                </Grid.Height>

            ...

이 작업을 수행 할 수 있습니까?

도움이 되었습니까?

해결책 3

실제로, 내가 생각한 것을 해결하기가 더 쉬웠습니다. 나는 컨트롤 플레이트를 가지고 있었기 때문에 TabControl 어쨌든, 나는 높이를 설정했다 ContentPresenter 선택한 탭 내용을 제시합니다. 나는 TabControl, 필요한 경우 측정하십시오 (사용 Measure)) 및 점검 DesiredSize 필요한 크기에 대해.

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var items = value as ItemCollection;

        if (items == null)
            return null;

        double max = 0;
        foreach (TabItem item in items)
        {
            var content = item.Content as FrameworkElement;
            if (content == null) continue;

            if (!content.IsMeasureValid)
                content.Measure(new Size(int.MaxValue, int.MaxValue));

            var height = content.DesiredSize.Height;
            if (max < height)
                max = height;
        }

        return max;
    }

그것은 약간의 경고와 함께 잘 작동합니다.

  • 모든 탭 컨텐츠는 a이어야합니다 FrameworkElement
  • 내용은로드되면 크기가 변경되지 않습니다 (변환기는 항목 속성이 한 번만 변경 될 때만 호출되기 때문에).

다른 팁

예, 할 수 있습니다. 해당 자리에 대한 재사용 그리드-로드 파티 템

예시:

    <TabControl Grid.IsSharedSizeScope="True">
        <TabItem Header="Tab 1">
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition SharedSizeGroup="xxx"/>
                </Grid.RowDefinitions>
            </Grid>
        </TabItem>
        <TabItem Header="Tab 2">
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition SharedSizeGroup="xxx"/>
                </Grid.RowDefinitions>
            </Grid>
        </TabItem>
   </TabControl>

문제는 TabControl 탭을 전환 할 때 컨텐츠를 언로드하고 다시로드합니다. 따라서 현재 활성 탭에서 컨텐츠의 크기에 대해서만 알고 있습니다. Tabcontrol을 변경하여 절대 아이들을 파괴하고 항상 존재하지만 (어쩌면 숨겨져있을 수도 있습니다).

이 블로그 게시물 에릭 버크 (Eric Burke)는 당신을 시작해야합니다. 그의 게시물을 훑어 보면서 말할 수있는 것에서 다음과 같이 변경해야합니다.

  • 모든 어린이는있을 때로드됩니다 TabControl 로드되었습니다.
  • 아이들은 비활성 상태 일 때 무너지기보다는 숨겨져 있습니다.

아마도 적절한 WPF 방식이 아닐 수도 있지만, 이미 모든 컨텐츠 요소를 가지고 있다면로드에서 고리를 통과하고 높이를 설정할 수 있습니다. TabControl 프로그램 적으로.

이것은 저와 함께 저에게 효과적이었습니다 Grid.IsSharedSizeScope 위에 표시된 접근법.

주목하십시오 SetCurrentValue 단지 설정하는 대신 사용됩니다 SelectedIndex 속성 -이 방법으로 기존의 바인딩을 유지합니다.

private void TabControl_OnLoaded(object sender, RoutedEventArgs e)
{
    //NOTE: loop through tab items to force measurement and size the tab control to the largest tab
    TabControl tabControl = (TabControl)sender;

    // backup selection
    int indexItemLast = tabControl.SelectedIndex;

    int itemCount = tabControl.Items.Count;

    for (
        int indexItem = (itemCount - 1);
        indexItem >= 0;
        indexItem--)
    {
        tabControl.SetCurrentValue(Selector.SelectedIndexProperty, indexItem);
        tabControl.UpdateLayout();
    }

    // restore selection
    tabControl.SetCurrentValue(Selector.SelectedIndexProperty, indexItemLast);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top