按“AddLexemesFromFolder”按钮后,网格变小

问题描述 投票:0回答:1

我开发了一个单页的应用程序。这是MainPage的XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Vertical" Grid.Row="0">
        <AppBar x:Name="MenuAppBar" IsOpen="True">
            <StackPanel Orientation="Horizontal">
                <AppBarButton Icon="Add" Label="Добавить лексемы" Name="AddLexemesFromFolder" Click="OpenFolderAndGetLexemes_Click" HorizontalAlignment="Left"/>
                <AppBarButton Icon="Save" Label="Сохранить лексемы" Name="SaveLexemes" Click="SaveLexemes_Click" HorizontalAlignment="Left"/>
            </StackPanel>
        </AppBar>
    </StackPanel>
    <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" VerticalScrollMode="Enabled">
        <Grid x:Name="GridLexemesViewer" HorizontalAlignment="Stretch"/>
    </ScrollViewer>
</Grid>

当我按下“AddLexemesFromFolder”按钮两次以上时,GridLexemesViewer反复变小。这是OpenFolderAndGetLexemes代码

 private async void OpenFolderAndGetLexemes_Click(object sender, RoutedEventArgs routedEventArgs)
    {
        await StartSaveLexemes();
        var folderPicker = new Windows.Storage.Pickers.FolderPicker();
        folderPicker.FileTypeFilter.Add("*");
        Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
        if (folder != null)
        {
            StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
            await Task.Run(() => StartNewSessionForGetLexemes(folder.Path));
            InitializeGrid();
        }
    }

我在GridLexemesViewer中使用“InitializeGrid”方法清除Children,使用CreateRowsAndColumns并将带有内容的TextBox放到GridLexemesViewer中。这是InitializeGrid和CreateRowsAndColumns()的代码

private void InitializeGrid()
    {
        GridLexemesViewer.Children.Clear();
        CreateRowsAndColumns();
        int index = 1;
        foreach (var lexem in CurrentSession.Lexemes)
        {
            foreach (var item in lexem.Value)
            {
                Binding binding = new Binding
                {
                    Source = item,
                    Path = new PropertyPath("Value"),
                    Mode = BindingMode.TwoWay,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                };
                TextBox textBox = new TextBox { TextWrapping = TextWrapping.Wrap };
                BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);
                GridLexemesViewer.Children.Add(textBox);
                Grid.SetColumn(textBox, CurrentSession.Languages.IndexOf(item.Language) + 1);
                Grid.SetRow(textBox, index);
            }
            index++;
        }

    }

    private void CreateRowsAndColumns()
    {
        int indexRow = 1;
        int indexColumn = 1;
        RowDefinition firstRowDefinition = new RowDefinition();
        ColumnDefinition firstColumnDefinition = new ColumnDefinition { Width = GridLength.Auto };
        GridLexemesViewer.ColumnDefinitions.Add(firstColumnDefinition);
        GridLexemesViewer.RowDefinitions.Add(firstRowDefinition);
        foreach (var key in CurrentSession.Lexemes.Keys)
        {
            RowDefinition rowDefinition = new RowDefinition();
            GridLexemesViewer.RowDefinitions.Add(rowDefinition);
            TextBlock textBlock = new TextBlock{Text = key};
            GridLexemesViewer.Children.Add(textBlock);
            Grid.SetRow(textBlock, indexRow);
            indexRow++;
        }
        foreach (var language in CurrentSession.Languages)
        {
            ColumnDefinition columnDefinition = new ColumnDefinition { Width = new GridLength(1.0, GridUnitType.Star)};
            GridLexemesViewer.ColumnDefinitions.Add(columnDefinition);
            TextBlock textBlock = new TextBlock {Text = language};
            GridLexemesViewer.Children.Add(textBlock);
            Grid.SetRow(textBlock, 0);
            Grid.SetColumn(textBlock, indexColumn);
            indexColumn++;
        }
    }

这个GIF显示了如何重现bug Reproduction of bug

c# uwp grid uwp-xaml
1个回答
1
投票

问题是你每次都在调用CreateRowsAndColumns()但是没有从之前的运行中删除RowsColumns。使用Grid.Clear()只删除Grid中的子控件,但Grid.RowDefinitionsGrid.ColumnDefinitions保持不变。

要解决这个问题,请在CreateRowsAndColumns()开头清除这两个定义:

GridLexemesViewer.RowDefinitions.Clear();
GridLexemesViewer.ColumnDefinitions.Clear();

但是,一定要考虑使用Windows社区工具包中的DataGrid control,因为它应该具有您需要的所有功能,并且具有更好的可维护性和性能,然后是自定义Grid,尤其是对于更大的数据。

© www.soinside.com 2019 - 2024. All rights reserved.