我在主网格中有一个细节网格。但是,内部网格被放入数据模板中,因此我无法在后面的代码中访问它。我想在代码中设置 Innergrid 列宽,并隐藏 InnerGrid 的第一列。稍后我会将三个网格控件(TopModelDatagrid、OptionDatagrid、InnerGridcontrol)的拖放功能添加到新的网格控件中。因此我需要从代码中访问它。谁能建议我如何在代码中访问它?
主窗口.xaml
<dx:ThemedWindow
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:local="clr-namespace:Master_Detail"
Title="MainWindow" Height="800" Width="1000">
<Grid>
<Grid Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="27*"/>
<RowDefinition Height="347*"/>
<RowDefinition Height="22*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Margin="0,0,0,0" Grid.RowSpan="2">
<Grid.RowDefinitions>
<RowDefinition Height="43*"/>
<RowDefinition Height="54*"/>
</Grid.RowDefinitions>
<dxg:GridControl x:Name="TopModelDatagrid" SelectionMode="Row" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" Margin="0,0,0,0" >
<dxg:GridControl.View>
<dxg:TableView x:Name="TopModelTableView" AutoWidth="True"/>
</dxg:GridControl.View>
</dxg:GridControl>
<dxg:GridControl x:Name="OptionDatagrid" SelectionMode="Row" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" Margin="0,0,0,0" Grid.Row="1">
<dxg:GridControl.View>
<dxg:TableView
x:Name="OptionTableView"
AllowMasterDetail="True"
AutoWidth="True"
IsDetailButtonVisibleBinding="{Binding Row.Opt, Converter={local:IsTopModelConverter}}"/>
</dxg:GridControl.View>
<dxg:GridControl.DetailDescriptor>
<dxg:ContentDetailDescriptor>
<dxg:ContentDetailDescriptor.ContentTemplate>
<DataTemplate>
<dxg:GridControl AutoGenerateColumns="AddNew" x:Name="InnerGrid" MaxHeight="1000"
ItemsSource="{Binding MasterRowData.View.Grid.ItemsSource, RelativeSource={RelativeSource TemplatedParent}}"
FixedFilter="{Binding Opt, Converter={local:FilterConverter}}">
<dxg:GridControl.View>
<dxg:TableView x:Name="InnerGridTableView" ShowGroupPanel="False" AutoWidth="True"/>
</dxg:GridControl.View>
</dxg:GridControl>
</DataTemplate>
</dxg:ContentDetailDescriptor.ContentTemplate>
</dxg:ContentDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
</dxg:GridControl>
</Grid>
</Grid>
</Grid>
</dx:ThemedWindow>
主窗口.xaml.vb
Imports System.Text
Imports DevExpress.Xpf.Core
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System
Imports System.IO
Imports Microsoft.Win32
Imports System.Windows
Imports System.ComponentModel
Imports DevExpress.Xpf.Grid
Imports System.Windows.Markup
Imports System.Globalization
Imports DevExpress.Data.Filtering
''' <summary>
''' Interaction logic for MainWindow.xaml
''' </summary>
Partial Public Class MainWindow
Inherits ThemedWindow
'-------------------Define Product Class--------------------------
Public Class Product
Public Property Partnumber As String
Public Property Opt As String
Public Property Description As String
Public Property USD As String
Public Sub New(PN As String, OP As String, Desc As String, UD As String)
Partnumber = PN
Opt = OP
Description = Desc
USD = UD
End Sub
End Class
Public Sub New()
InitializeComponent()
LoadBound(System.Environment.CurrentDirectory & "\SourceData.txt")
TopModelDatagrid.ItemsSource = TopProducts
OptionDatagrid.ItemsSource = OptionProducts
'InnerGrid.ItemsSource = OptionProducts
TopModelDatagrid.CurrentItem = TopModelDatagridRowNumber
OptionDatagrid.Columns(0).AutoFilterValue = TopModelDatagrid.GetCellValue(0, "Partnumber")
'InnerGrid.Columns(0).AutoFilterValue = OptionDatagrid.GetFocusedRowCellValue("Opt")
OptionDatagrid.Columns(0).Visible = False
'InnerGrid.Columns(0).Visible = False
TopModelDatagrid.Columns(1).Visible = False
End Sub
Dim TopProducts As ObservableCollection(Of Product)
Dim OptionProducts As ObservableCollection(Of Product)
Dim TopModelDatagridRowNumber As Integer = 0
Dim OptionDatagridRowNumber As Integer = 0
Property DatagridFocus As ObservableCollection(Of Boolean) = New ObservableCollection(Of Boolean)
Private Sub LoadBound(ByVal fName As String)
'Build a List(Of Product) from the text file
Dim lstProducts As New List(Of Product)
Dim lst2Products As New List(Of Product)
Dim lines = File.ReadAllLines(fName, System.Text.Encoding.Default)
Dim templine As String
Dim i As Integer = 0
For Each line In lines
i = i + 1
templine = line
Dim Props() As String
Props = templine.Split(CChar(vbTab))
If Props(0) = Props(1) Then
Dim p As New Product(Props(0), Props(1), Props(2), Props(3))
lstProducts.Add(p)
Else
Dim p As New Product(Props(0), Props(1), Props(2), Props(3))
lst2Products.Add(p)
End If
Next
'The constructor of an ObservableCollection can take a List(Of T)
TopProducts = New ObservableCollection(Of Product)(lstProducts)
OptionProducts = New ObservableCollection(Of Product)(lst2Products)
End Sub
Private Sub TopModelDatagrid_CurrentItemChanged(sender As Object, e As CurrentItemChangedEventArgs) Handles TopModelDatagrid.CurrentItemChanged
Try
OptionDatagrid.Columns(0).AutoFilterValue = TopModelDatagrid.GetFocusedRowCellValue("Partnumber")
Catch ex As Exception
End Try
End Sub
Private Sub OptionDatagrid_CurrentItemChanged(sender As Object, e As CurrentItemChangedEventArgs) Handles OptionDatagrid.CurrentItemChanged
Try
'InnerGrid.Columns(0).AutoFilterValue = OptionDatagrid.GetFocusedRowCellValue("Opt")
Catch ex As Exception
End Try
End Sub
End Class
Public Class IsTopModelConverter
Inherits MarkupExtension
Implements IValueConverter
Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
Return Me
End Function
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
Return value.ToString().StartsWith("Topmodel")
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
Public Class FilterConverter
Inherits MarkupExtension
Implements IValueConverter
Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
Return Me
End Function
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
Return New BinaryOperator("Partnumber", value)
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class