Hi.
I found a solution.
Below code works perfect. However, I think there is better way for this scenario. For example I think there could be a way to use CallBack and Filtering. But I do not have any idea.
So please kindly help me to make a better code.
I notice again that this code works perfect.
<Window x:Class="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:WpfApp13TEMP"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DataGrid x:Name="DGCheckingProjector"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
BorderThickness="0"
Foreground="White"
Background="gray"
RowBackground="Transparent"
FontSize="16"
FontWeight="Light"
GridLinesVisibility="Horizontal"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="True"
CanUserResizeRows="True"
CanUserResizeColumns="True"
CanUserSortColumns="True"
IsTextSearchEnabled="True"
ColumnWidth="Auto"
AutoGenerateColumns="False"
SelectionMode="Single"
SelectionUnit="Cell"
IsReadOnly="False"
FlowDirection="LeftToRight">
<DataGrid.Columns>
<DataGridTextColumn Header="" IsReadOnly="True" Width="2*" Binding="{Binding IndexNo}"/>
<DataGridTextColumn Header="TheText" IsReadOnly="False" Width="26*" Binding="{Binding TheText}"/>
<DataGridTextColumn Header="Dubber" IsReadOnly="False" Width="12*" Binding="{Binding DubberName}"/>
<DataGridTextColumn Header="Character" IsReadOnly="False" Width="12*" Binding="{Binding CharacterName}"/>
<DataGridTextColumn Header="Gender" IsReadOnly="False" Width="10*" Binding="{Binding GenderOfCharacter}"/>
</DataGrid.Columns>
</DataGrid>
<TextBlock x:Name="tbSTATUS" HorizontalAlignment="Left" Margin="114,196,0,0" TextWrapping="Wrap" Text="" FontSize="26" Background="White" VerticalAlignment="Top" Height="106" Width="297"/>
</Grid>
</Window>
Class MainWindow
Public Class SubtitleClass
Public Property FlagOfSubInVideo As New Boolean
Public Property IndexNo As Integer
Public Property StartingTimeCode As String
Public Property EndingTimeCode As String
Public Property TheText As String
Public Property DubberName As String
Public Property CharacterName As String
Public Property GenderOfCharacter As String
End Class
Public MySub As New System.Collections.ObjectModel.ObservableCollection(Of SubtitleClass)
Private UpperRowIndexToInsert As String = Nothing
Private LowerRowIndexToInsert As String = Nothing
Sub New()
InitializeComponent()
Dim Fname As String(), Lname As String(), Cname As String()
ReDim Preserve Fname(10), Lname(10), Cname(10)
Fname(1) = "Ali" : Fname(2) = "Morteza" : Fname(3) = "Reza" : Fname(4) = "Kazem" : Fname(5) = "Hassan" : Fname(6) = "Hossein" : Fname(7) = "Farhad" : Fname(8) = "Shervin" : Fname(9) = "" : Fname(10) = "Mohammad"
Lname(1) = "Alavi" : Lname(2) = "Ghalichi" : Lname(3) = "Hosseini" : Lname(4) = "Kazemi" : Lname(5) = "Kordani" : Lname(6) = "Tehrani" : Lname(7) = "Moghadam" : Lname(8) = "Haddadi" : Lname(9) = "" : Lname(10) = "Karimian"
Cname(1) = "Arnold" : Cname(2) = "Richard" : Cname(3) = "Big Man" : Cname(4) = "Agent Smith" : Cname(5) = "Prisoner" : Cname(6) = "Mr. Anderson" : Cname(7) = "Kenneth Williams" : Cname(8) = "Eddie Morphy" : Cname(9) = "William Marchoma" : Cname(10) = "Queentin Tarantino"
Randomize()
For I As Integer = 1 To 100
MySub.Add(New SubtitleClass With {
.FlagOfSubInVideo = Int(2 * Rnd()),
.IndexNo = Int((1255) * Rnd() + 1) + I,
.StartingTimeCode = "12:34:56:789",
.EndingTimeCode = "21:43:65:987",
.TheText = $"This is the Text of Subtitle number {I}",
.DubberName = Fname(Int(10 * Rnd() + 1)) & " " & Lname(Int(10 * Rnd() + 1)),
.CharacterName = Cname(Int(10 * Rnd() + 1)),
.GenderOfCharacter = If((Int(2 * Rnd())), "Male", "Female")
})
Next I
DGCheckingProjector.ItemsSource = MySub
btnPLUS.Visibility = Visibility.Hidden
End Sub
Private Sub DGCheckingProjector_PreviewMouseMove(sender As Object, e As MouseEventArgs) Handles DGCheckingProjector.PreviewMouseMove
Dim MyDataGrid As DataGrid = TryCast(sender, DataGrid)
Dim pt As Point = e.GetPosition(MyDataGrid)
If MyDataGrid IsNot Nothing Then
Dim hitTestResult As HitTestResult = VisualTreeHelper.HitTest(MyDataGrid, New Point(0, pt.Y))
Dim hitTestResultU As HitTestResult = VisualTreeHelper.HitTest(MyDataGrid, New Point(0, pt.Y - 10))
Dim hitTestResultD As HitTestResult = VisualTreeHelper.HitTest(MyDataGrid, New Point(0, pt.Y + 10))
Dim row As DataGridRow = If(hitTestResult IsNot Nothing, TryCast(DataGridRow.GetRowContainingElement(hitTestResult.VisualHit), DataGridRow), Nothing)
Dim rowU As DataGridRow = If(hitTestResultU IsNot Nothing, TryCast(DataGridRow.GetRowContainingElement(hitTestResultU.VisualHit), DataGridRow), Nothing)
Dim rowD As DataGridRow = If(hitTestResultD IsNot Nothing, TryCast(DataGridRow.GetRowContainingElement(hitTestResultD.VisualHit), DataGridRow), Nothing)
Dim bool As Boolean = False
If hitTestResult IsNot Nothing Then
If hitTestResult.VisualHit.DependencyObjectType.Name = "Border" Then
If row IsNot Nothing Then
If rowU IsNot Nothing And rowD IsNot Nothing Then
If String.Equals(row.GetIndex, rowU.GetIndex) Or String.Equals(row.GetIndex, rowD.GetIndex) Then
bool = True
UpperRowIndexToInsert = rowU.GetIndex
LowerRowIndexToInsert = rowD.GetIndex
End If
ElseIf rowD Is Nothing Then
If row.GetIndex = MyDataGrid.Items.Count - 1 Then UpperRowIndexToInsert = rowU.GetIndex : LowerRowIndexToInsert = Nothing : bool = True
End If
End If
End If
End If
If hitTestResultU IsNot Nothing And Not bool Then
If hitTestResultU.VisualHit.DependencyObjectType.Name = "Rectangle" Then
If row IsNot Nothing Then
If row.GetIndex = 0 Then
UpperRowIndexToInsert = Nothing
LowerRowIndexToInsert = rowD.GetIndex
bool = True
Else
UpperRowIndexToInsert = row.GetIndex - 1
LowerRowIndexToInsert = row.GetIndex
bool = True
End If
End If
End If
End If
If bool Then
btnPLUS.Visibility = Visibility.Visible
tbSTATUS.Text = "Beyne " & If(UpperRowIndexToInsert, "Null") & " & " & If(LowerRowIndexToInsert, "Null")
btnPLUS.Margin = New Thickness(btnPLUS.Margin.Left, -btnPLUS.RenderSize.Height / 2, btnPLUS.Margin.Right, btnPLUS.Margin.Bottom)
Dim myTranslateTransform As TranslateTransform = New TranslateTransform
If rowD IsNot Nothing Then
myTranslateTransform.Y = rowD.TranslatePoint(New Point(0, 0), DGCheckingProjector).Y
ElseIf row IsNot Nothing Then
myTranslateTransform.Y = row.TranslatePoint(New Point(0, 0), DGCheckingProjector).Y + row.RenderSize.Height
End If
btnPLUS.RenderTransform = myTranslateTransform
Else
btnPLUS.Visibility = Visibility.Hidden
tbSTATUS.Text = ""
End If
End If
End Sub
Private Sub BtnPLUS_Click(sender As Object, e As RoutedEventArgs) Handles btnPLUS.Click
Dim newRow As New SubtitleClass With {
.FlagOfSubInVideo = Int(2 * Rnd()),
.IndexNo = "500",
.StartingTimeCode = "11:22:33:456",
.EndingTimeCode = "44:55:66:789",
.TheText = $"This is new item",
.DubberName = "M",
.CharacterName = "N",
.GenderOfCharacter = If((Int(2 * Rnd())), "Male", "Female")
}
If LowerRowIndexToInsert IsNot Nothing Then
MySub.Insert(LowerRowIndexToInsert, newRow)
DGCheckingProjector.ScrollIntoView(DGCheckingProjector.Items(LowerRowIndexToInsert))
Else
MySub.Add(newRow)
DGCheckingProjector.ScrollIntoView(DGCheckingProjector.Items(DGCheckingProjector.Items.Count - 1))
End If
End Sub
End Class