GroupItem Expander 中 ScrollViewer 的 WPF DataGrid 问题

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

我不知道如何解决它,谁能解释一下,好吗?

我有带扩展器的 DataGrid 分组项目。我想在扩展器中有一个滚动条,但是如果我将 CanUserAddRows="false" 设置为 DataGrid,它

s get an error: "The specified Visual is not an ancestor of this Visual." I don
t 想要允许用户添加行。如何解决?

我的例子:

        ObservableCollection<User> users = new ObservableCollection<User>();
        users.Add(new User() { Group = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23) });
        users.Add(new User() { Group = 2, Name = "Jane Doe1", Birthday = new DateTime(1974, 1, 17) }); 
        users.Add(new User() { Group = 3, Name = "Sammy Doe1", Birthday = new DateTime(1991, 9, 2) }); 

为群组添加更多项目

       ItemsViewCollection = CollectionViewSource.GetDefaultView(users);
       ItemsViewCollection.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
       dGrid.ItemsSource = ItemsViewCollection;
    public class User
    {
        public int Group { get; set; }
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }

Xaml

 <Grid>
    <DataGrid x:Name="dGrid" HorizontalAlignment="Left" CanUserDeleteRows="False" CanUserAddRows="False">
        <DataGrid.GroupStyle >
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True">
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock Text="{Binding ItemCount, StringFormat= Count: {0}}"  Margin="30,0,0,0" />
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <Grid>
                                                <ScrollViewer Margin="20,5,10,5"  MaxHeight="60"
                                                              CanContentScroll="False" 
                                                              VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled">
                                                    <Grid>
                                                        <ItemsPresenter  Margin="0 0 0 10" Cursor="Arrow" />
                                                    </Grid>
                                                </ScrollViewer>
                                            </Grid>
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
    </DataGrid>
</Grid>

堆栈跟踪:

System.InvalidOperationException
HResult=0x80131509
Message = The specified Visual is not an ancestor of this Visual.
Source = PresentationCore
Stack trace:
in System.Windows.Media.Visual.TrySimpleTransformToAncestor(Visual ancestor, Boolean inverse, GeneralTransform& generalTransform, Matrix& simpleTransform)
in System.Windows.Media.Visual.TransformToAncestor(Visual ancestor)
in System.Windows.Controls.DataGridCellsPanel.ComputeCellsPanelHorizontalOffset()
in System.Windows.Controls.DataGridHelper.GetParentCellsPanelHorizontalOffset(IProvideDataGridColumn cell)
in System.Windows.Controls.DataGrid.InvalidateCellsPanelHorizontalOffset(Object args)
in System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
in System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
in System.Windows.Threading.DispatcherOperation.InvokeImpl()
in System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
in MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
c# wpf datagrid expander
© www.soinside.com 2019 - 2024. All rights reserved.