1

对不起,我的英语不好。请帮帮我,我需要在 RowDetailsTemplate 中为表格设置选定的项目。Xml代码:

<Grid>
            <DataGrid 
            Name="GvHeader"                     
            AutoGenerateColumns="False"                                                 
            HorizontalScrollBarVisibility="Auto"
            CanUserDeleteRows="False"  
            CanUserAddRows="False"            
            CanUserResizeColumns="True"
            CanUserReorderColumns="False"
            CanUserResizeRows="False"
            AllowDrop="False"
            RowDetailsVisibilityMode="Collapsed"
            IsReadOnly="True" ClipboardCopyMode="ExcludeHeader" SelectionMode="Single" SelectionChanged="GvHeader_SelectionChanged">
                        <DataGrid.RowStyle>
                            <Style TargetType="{x:Type DataGridRow}">
                                <EventSetter Event="MouseDoubleClick" Handler="HeaderRowDoubleClick"/>
                                <EventSetter Event="Unselected" Handler="HeaderRowUnselected"></EventSetter>
                            </Style>
                        </DataGrid.RowStyle>
                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <DataGrid x:Name="GvLines"                    
                                AutoGenerateColumns="False"                                                 
                                HorizontalScrollBarVisibility="Auto"
                                CanUserDeleteRows="False"  
                                CanUserAddRows="False"            
                                CanUserResizeColumns="True"
                                CanUserReorderColumns="False"
                                CanUserResizeRows="False"
                                AllowDrop="False"            
                                IsReadOnly="True" ClipboardCopyMode="ExcludeHeader" SelectionMode="Single" ItemsSource="{Binding Path=Lines}"
                                SelectionChanged="GvLines_SelectionChanged" MouseDoubleClick="GvLines_OnMouseDoubleClick">
                                <DataGrid.Columns>
                                    <DataGridTemplateColumn x:Name="ShowLineStatus"                                           
                                       CanUserSort="False"                                           
                                       IsReadOnly="True"                                                                                                                                                         
                                       Width="22">

                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <Image Width="20" Name="pop" Height="20" ToolTip="Восстановить" Source="/Management;component/Images/key_enter.png">
                                                </Image>
                                                <DataTemplate.Triggers>
                                                    <DataTrigger Binding="{Binding CancelSatatus}" Value="False">
                                                        <Setter TargetName="pop"  Property="Source" Value="/Management;component/Images/cancel.png"/>
                                                        <Setter TargetName="pop" Property="ToolTip" Value="Отменить"/>
                                                    </DataTrigger>
                                                </DataTemplate.Triggers>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>

                                    <DataGridTextColumn
                                        x:Name="ColumnNumber"
                                        Header="oooo1"
                                        MinWidth="50" Width="Auto"                                            
                                        Binding="{Binding Path=line1,Mode=OneWay}"/>

                                    <DataGridTextColumn
                                        Header="ooo2"                                                                                  
                                        MinWidth="20" Width="Auto" 
                                        CellSt yle="{StaticResource CellStyleCenter}"
                                        Binding="{Binding Path=line2,Mode=OneWay}"/>
                                    <DataGridTextColumn
                                        Header="00003"
                                        MinWidth="50" Width="Auto"                                            
                                        Binding="{Binding Path=line3,Mode=OneWay}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                        </DataTemplate>
                </DataGrid.RowDetailsTemplate>
                <DataGrid.Columns>
                    <DataGridTextColumn
                                    x:Name="Number"
                                    Header="Номер"
                                    MinWidth="50" Width="Auto"                                            
                                    Binding="{Binding Path=head1,Mode=OneWay}"/>
                    <DataGridTextColumn
                                    Header="Sss"                                                                                  
                                    MinWidth="20" Width="Auto" 
                                    CellSt yle="{StaticResource CellStyleCenter}"
                                    Binding="{Binding Path=head2,Mode=OneWay}"/>
                    </DataGrid.Columns>
            </DataGrid>
        </Grid>

用于测试的代码 C#:

var line1 = new List<ILine>
            {
                new Line("head11","h1line11", "h1line12", "h1line13", "h1line14"),
                new Line("head11","h1line21", "h1line22", "h1line23", "h1line24"),
                new Line("head11","h1line31", "h1line32", "h1line33", "h1line34"),
                new Line("head11","h1line41", "h1line42", "h1line43", "h1line44")
            };

        var line2 = new List<ILine>
            {
                new Line("head21","h2line11", "h2line12", "h2line13", "h2line14"),
                new Line("head21","h2line21", "h2line22", "h2line23", "h2line24"),
                new Line("head21","h2line31", "h2line32", "h2line33", "h2line34")
            };
        var header = new List<Header>
            {
                new Header("head11", "head12", "head13", "head14"),
                new Header("head21", "head22", "head23", "head24")
            };


        header[0].SetLines(line1);
        header[1].SetLines(line2);

        Global_H_items.AddRange(header);
        GvHeader.ItemsSource = null;
        GvHeader.ItemsSource = Global_H_items;

我需要像这样访问表“GvLines”(这是我的锁定/解锁界面方法,解锁后我需要设置选择标题项和行项)。

public void IsBlock(bool value, IHeader sel_header, ILine sel_line)
    {
        if (!this.Dispatcher.CheckAccess())
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action<Boolean, IHeader, ILine>(IsBlock), value, sel_header);
            return;
        }

        GridMain.IsEnabled = value;

        if (sel_header != null)
        {
            GvHeader.ScrollIntoView(sel_header);
            GvHeader.SelectedItem = sel_header;

        }

        if (sel_line != null)
        {
            var row = (DataGridRow)GvHeader.ItemContainerGenerator.ContainerFromIndex(GvHeader.SelectedIndex);
            row.DetailsVisibility = Visibility.Visible;

            //Here I need take dgLines table and set selected item. I don't know how make this.
            if (dgLines != null)
            {
                dgLines.ScrollIntoView(sel_line);
                dgLines.SelectedItem = sel_line;
            }
        }

}

4

0 回答 0