Гость
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Странное поведение Grid / 7 сообщений из 7, страница 1 из 1
27.09.2017, 19:05
    #39527274
AltProg
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Странное поведение Grid
Добрый день!

Возникла непонятная ситуация с поведением Grid, а именно меняется явно установленный размер колонки пропорционально количеству TabItem`ов в TabControl.

В производственном коде найдено простое решение, но хотелось бы выяснить причину такого поведения.



Если раскрыть вторую и третью строку нажатием на кнопку >>, то все ожидаемо.



Но если раскрыть первую строку, то размер колонки увеличивается, хотя ColumnSpan дочернего TabControl равен трем.



Код ItemCollection
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

namespace WpfApplication5
{
    public class ItemCollection
    {
        public ItemCollection()
        {
            Items = new List<Item>
            {
                new Item
                {
                    Id = 1,
                    Name = "Item 1",
                    SubItems = new List<SubItem>
                    {
                        new SubItem { Name = "SubItem 11" },
                        new SubItem { Name = "SubItem 12" },
                        new SubItem { Name = "SubItem 13" }
                    }
                },
                new Item
                {
                    Id = 2,
                    Name = "Item 2",
                    SubItems = new List<SubItem>
                    {
                        new SubItem { Name = "SubItem 21" }
                    }
                },
                new Item
                {
                    Id = 3,
                    Name = "Item 3",
                    SubItems = new List<SubItem>
                    {
                        new SubItem { Name = "SubItem 31" }
                    }
                }
            };
        }

        public IEnumerable<Item> Items { get; set; }
    }

    public class Item : BindableBase
    {
        public Item()
        {
            ExpandCommand = new DelegateCommand<object>(OnExpandCommandExecuted);
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsExpanded { get; set; }
        public IEnumerable<SubItem> SubItems { get; set; }

        public ICommand ExpandCommand { get; }

        private void OnExpandCommandExecuted(object obj)
        {
            IsExpanded = !IsExpanded;
            RaisePropertyChanged(nameof(IsExpanded));
        }
    }

    public class SubItem
    {
        public string Name { get; set; }
    }

    public class BooleanVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(Visibility))
            {
                throw new InvalidOperationException();
            }

            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(bool))
            {
                throw new InvalidOperationException();
            }

            return (Visibility)value == Visibility.Visible;
        }
    }
}



Код MainWindow
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
<Window x:Class="WpfApplication5.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication5"
        mc:Ignorable="d" 
        Title="MainWindow" Height="400" Width="600">
    <Window.DataContext>
        <local:ItemCollection />
    </Window.DataContext>
    <Grid>
        <ListBox 
            HorizontalContentAlignment="Stretch"
            ItemsSource="{Binding Items}">
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type local:Item}">
                    <local:ItemView/>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>
    </Grid>
</Window>



Код ItemView
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
<UserControl x:Class="WpfApplication5.ItemView"
             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:local="clr-namespace:WpfApplication5"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <local:BooleanVisibilityConverter
            x:Key="myConverter" />
    </UserControl.Resources>
    
    <Grid
        x:Name="grid"
        ShowGridLines="True"
        Background="#E9E9E9">
        
        <Grid.RowDefinitions>
            <RowDefinition
                Height="50"/>
            <RowDefinition
                Height="Auto"/>
            <RowDefinition
                Height="Auto"/>
        </Grid.RowDefinitions>
        
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="70" />
            <ColumnDefinition Width="70" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Button
            Content=">>"
            Command="{Binding ExpandCommand}"/>

        <TextBlock 
            Grid.Column="1"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            Text="{Binding Id}" />

        <TextBlock 
            Grid.Column="2"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            Text="{Binding Name}" />

        <TabControl
            Grid.Column="0"
            Grid.Row="1"
            Grid.ColumnSpan="3"
            Visibility="{Binding IsExpanded, Converter={StaticResource myConverter}}"
            ItemsSource="{Binding SubItems}" />
        
    </Grid>
</UserControl>




Кто-нибудь знает причину изменения ширины колонки?
Спасибо.

PS. Также подключен Nuget Prism.Core
...
Рейтинг: 0 / 0
27.09.2017, 20:13
    #39527296
AltProg
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Странное поведение Grid
Fix картинок.

Картинки



...
Рейтинг: 0 / 0
27.09.2017, 21:17
    #39527309
Roman Mejtes
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Странное поведение Grid
это же у вас пример, закидывайте сюда и посмотрим ) так чет лень вникать, сообщение какое то мутные и непонятное
...
Рейтинг: 0 / 0
27.09.2017, 22:35
    #39527325
AltProg
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Странное поведение Grid
Кинул.
То не сообщения, в TabItem`ах, в Heder`ах просто имена классов выводится, для минимизации кода.
В общем вопрос, почему поведение в первой строке не такое как во второй и третьей.
...
Рейтинг: 0 / 0
28.09.2017, 05:06
    #39527391
Sprinter1234
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Странное поведение Grid
Спасибо за приглашение на форуме
...
Рейтинг: 0 / 0
28.09.2017, 16:24
    #39527780
Roman Mejtes
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Странное поведение Grid
для ListBox'а задайте свойство
ScrollViewer.HorizontalScrollBarVisibility="Disabled"

Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
        <ListBox 
            HorizontalContentAlignment="Stretch"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled"
            ItemsSource="{Binding Items}">
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type local:Item}">
                    <local:ItemView/>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>
...
Рейтинг: 0 / 0
28.09.2017, 16:54
    #39527813
AltProg
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Странное поведение Grid
Roman Mejtes,

Спасибо большое, получилось).
...
Рейтинг: 0 / 0
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Странное поведение Grid / 7 сообщений из 7, страница 1 из 1
Целевая тема:
Создать новую тему:
Автор:
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]