I have a simple datagrid with some itemssource. I select a row and go to other control. Every time i focus the grid again using tab, it selects the first row and cell. (It show a black border box on first cell)
I have set the IsSynchronizedWithCurrentItem="True" but not working
I want the previous selected row to get focus.
"it selects the first row and cell" no in fact the selection remains as it was but yes when the focus returns to datagrid the focused cell will change to the first cell.
Check if this approach suits you, it's the simplest I came with so far!:
First, add event handler to cell GotFocus and LostFocus as follows:
<DataGrid x:Name="list_1">
<DataGrid.Columns >
<DataGridTextColumn Header="ID" Width="30" Binding="{Binding id}"/>
<DataGridTextColumn Header="Name" Width="50" Binding="{Binding name}"/>
<DataGridTextColumn Header="UserID" Width="50" Binding="{Binding user_id}"/>
</DataGrid.Columns>
<DataGrid.CellStyle >
<Style TargetType="DataGridCell" >
<EventSetter Event="GotFocus" Handler="cgf"/>
<EventSetter Event="LostFocus" Handler="clf"/>
</Style>
</DataGrid.CellStyle>
</DataGrid>
In your code behind, add the following code:
Private WithEvents timer1 As New Threading.DispatcherTimer With {.Interval = New TimeSpan(0, 0, 0, 0, 50)}
Private WithEvents timer2 As New Threading.DispatcherTimer With {.Interval = New TimeSpan(0, 0, 0, 0, 50)}
Dim LastFocusedCell As DataGridCell
Dim focus_lost As Boolean
Sub cgf(sender As object, e As RoutedEventArgs)
timer1.Stop()
If focus_lost Then
focus_lost = False
If LastFocusedCell IsNot Nothing Then
list_1.IsReadOnly = True
LastFocusedCell.Focus()
timer2.Start()
Else
LastFocusedCell = sender
End If
Else
LastFocusedCell = sender
End If
End Sub
Sub clf(sender As Object, e As RoutedEventArgs)
timer1.Start()
End Sub
Private Sub tmr_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
timer1.Stop()
focus_lost = True
End Sub
Private Sub tmr2_Tick(sender As Object, e As EventArgs) Handles timer2.Tick
timer2.stop()
list_1.IsReadOnly = False
End Sub
I don't think it's the best approach but it worked fine for me.
Related
I have a WPF application with a listview being represented by a gridview in the UI. I want to have an event that when a cell in this list view is clicked i can see the row and the column index of cell. Currently i can get the entire list item (the row) but not the column which is clicked.
For example my list view looks like this:
List view example
When the email address sammy.doe#gmail.com is clicked the row=2 and column=2 should be available in the event handler, and when the age 39 is clicked row=1 and column=1 should be available.
How is it possible to get this information in the click event handler.
Thanks in advance.
After working on this problem for a couple of day i have found a solution my using the mouse point location on a click event. And then searching through the Gridview columns to find which column this lays in by considering the X position of the pointer.
Heres my code behind:
Class MainWindow
Public Sub New()
InitializeComponent()
Dim items As List(Of User) = New List(Of User)
items.Add(New User() With {.Name = "Isaac", .Age = 23, .Email = "isaac#email.co.uk", .City = "London", .Phone = "123456789"})
items.Add(New User() With {.Name = "Jacob", .Age = 29, .Email = "jacob#email.co.uk", .City = "Royston", .Phone = "214563255"})
lv.ItemsSource = items
Me.DataContext = Me
End Sub
Private Sub Lv_PreviewMouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)
Dim gv = DirectCast(lv.View, GridView)
Dim lvi = DirectCast(sender, ListViewItem)
Dim p = e.GetPosition(lvi)
Dim c_n = GetColumnNumber(gv, p)
Debug.Print("Row: " + lv.SelectedIndex.ToString + " Column: " + c_n.ToString)
End Sub
Public Function GetColumnNumber(gv As GridView, p As Point) As Integer
Dim n As Integer = 0
Dim columnFound As Boolean = False
Dim leftSide = 0
Dim rightSide = 0
For Each c In gv.Columns
rightSide += c.ActualWidth
If InRange(leftSide, rightSide, p.X) Then
columnFound = True
Exit For
End If
leftSide = rightSide
n += 1
Next
If Not columnFound Then n = Nothing
Return n
End Function
Public Function InRange(lower As Double, upper As Double, value As Double) As Boolean
If value < upper And value > lower Then Return True
Return False
End Function
End Class
Public Class User
Public Property Name As String
Public Property Age As Integer
Public Property Email As String
Public Property Phone As String
Public Property City As String
End Class
And here is my Xaml:
<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:ListViewSelection"
mc:Ignorable="d"
Title="List View Selection" Height="450" Width="800">
<Grid>
<ListView Margin="10" Name="lv">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonUp" Handler="lv_PreviewMouseLeftButtonUp"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="auto" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Age" Width="auto" DisplayMemberBinding="{Binding Age}"/>
<GridViewColumn Header="Email" Width="auto" DisplayMemberBinding="{Binding Email}"/>
<GridViewColumn Header="Phone" Width="auto" DisplayMemberBinding="{Binding Phone}"/>
<GridViewColumn Header="City" Width="auto" DisplayMemberBinding="{Binding City}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
I hope this can help someone else.
Dim enteredtext AS STRING = mytextbox.text
mylabel.content = enteredtext
How can i make the entered text that a user enters in a textbox be printed to a label as either bold italic or underlined. These three options shall be enabled with a radio button.
You can set the font style for them.
e.g.:
mylabel.FontStyle = FontStyles.Italic
You can refer the below code.
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<RadioButton x:Name="rdBold" Checked="RadioButton_Checked" Content="Bold" GroupName="format"/>
<RadioButton x:Name="rdItalics" Checked="rdItalics_Checked" Content="Italics" GroupName="format"/>
<RadioButton x:Name="rdUnderline" Checked="rdUnderline_Checked" Content="Underline" GroupName="format"/>
</StackPanel>
<TextBox x:Name="txtBx" Width="200" Height="20"/>
<TextBlock x:Name="txtBlk" Text="{Binding ElementName=txtBx,Path=Text}"/>
</StackPanel>
</Grid>
Class MainWindow
Private Sub RadioButton_Checked(sender As Object, e As RoutedEventArgs)
txtBlk.FontWeight = FontWeights.Bold
txtBlk.FontStyle = FontStyles.Normal
txtBlk.TextDecorations = Nothing
End Sub
Private Sub rdItalics_Checked(sender As Object, e As RoutedEventArgs)
txtBlk.FontWeight = FontWeights.Normal
txtBlk.FontStyle = FontStyles.Italic
txtBlk.TextDecorations = Nothing
End Sub
Private Sub rdUnderline_Checked(sender As Object, e As RoutedEventArgs)
txtBlk.FontWeight = FontWeights.Normal
txtBlk.FontStyle = FontStyles.Normal
txtBlk.TextDecorations = TextDecorations.Underline
End Sub
End Class
As above if you are only doing this sometimes and wish to do it in the code behind you can use mylabel.FontStyle = FontStyles.Italic or if the label always needs to be bold you can look at Font section in the properties tab and choose colur, size, bold, italics, underline, strikeout etc from there.
I am developing a WPF application. In one of my pages I have a DockPanel with 3 ComboBoxes within it. These 3 ComboBoxes are dependent on each other. So, I have a binding group on the DockPanel and a validation rule for that binding group.
I would like to validate these 3 ComboBoxes when the DockPanel loses focus; however, the DockPanel's LostFocus event is fired when the user clicks on one of the child ComboBoxes within it!
I would have thought that if the child control has focus, the parent control would also have focus...however this does not seem to be the case.
I am looking for a solution to perform validation on the binding group when the DockPanel loses focus...where the focus is lost to a control that is not one of its children.
Edit:
So, I created as simple an application as I could to demonstrate the problem.
I have a "FertilizerCombination" class that contains 3 integers that make up the Fertilizer. These must be unique within the application. Currently the only combination that is not available is 10-10-10. This is a hard coded value in the class.
Public Class FertilizerCombination
Private _nitrogen As Integer
Private _phosphorous As Integer
Private _potassium As Integer
<System.ComponentModel.DataAnnotations.Range(1, 20)> _
Public Property Nitrogen As Integer
Get
Return _nitrogen
End Get
Set(value As Integer)
System.ComponentModel.DataAnnotations.Validator.ValidateProperty(value, NitrogenValidationContext)
_nitrogen = value
End Set
End Property
<System.ComponentModel.DataAnnotations.Range(1, 20)> _
Public Property Phosphorous As Integer
Get
Return _phosphorous
End Get
Set(value As Integer)
System.ComponentModel.DataAnnotations.Validator.ValidateProperty(value, PhosphorousValidationContext)
_phosphorous = value
End Set
End Property
<System.ComponentModel.DataAnnotations.Range(1, 20)> _
Public Property Potassium As Integer
Get
Return _potassium
End Get
Set(value As Integer)
System.ComponentModel.DataAnnotations.Validator.ValidateProperty(value, PotassiumValidationContext)
_potassium = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal nitrogen As Integer, ByVal phosphorous As Integer, ByVal potassium As Integer)
Me.Nitrogen = nitrogen
Me.Phosphorous = phosphorous
Me.Potassium = potassium
End Sub
Public Shared Function FertilizerCombinationAvailable(ByVal nitrogen As Integer, ByVal phosphorous As Integer, ByVal potassium As Integer) As Boolean
'Checking against combinations already used'
If nitrogen = "10" And phosphorous = "10" And potassium = "10" Then
'Combination has already been used'
Return False
End If
'Combination was not used yet'
Return True
End Function
Public Sub SetFertilizerCombination(ByVal nitrogen As Integer, ByVal phosphorous As Integer, ByVal potassium As Integer)
System.ComponentModel.DataAnnotations.Validator.ValidateProperty(nitrogen, NitrogenValidationContext)
System.ComponentModel.DataAnnotations.Validator.ValidateProperty(phosphorous, PhosphorousValidationContext)
System.ComponentModel.DataAnnotations.Validator.ValidateProperty(potassium, PotassiumValidationContext)
If FertilizerCombination.FertilizerCombinationAvailable(nitrogen, phosphorous, potassium) = False Then
Throw New ArgumentException("This fertilizer combination has already been used")
End If
Me.Nitrogen = nitrogen
Me.Phosphorous = phosphorous
Me.Potassium = potassium
End Sub
Private NitrogenValidationContext = New System.ComponentModel.DataAnnotations.ValidationContext(Me, Nothing, Nothing) With {.MemberName = "Nitrogen"}
Private PhosphorousValidationContext = New System.ComponentModel.DataAnnotations.ValidationContext(Me, Nothing, Nothing) With {.MemberName = "Phosphorous"}
Private PotassiumValidationContext = New System.ComponentModel.DataAnnotations.ValidationContext(Me, Nothing, Nothing) With {.MemberName = "Potassium"}
End Class
I have a created a Validation Rule for the Fertilizer Combination Class:
Public Class FertilizerCombinationValidationRule
Inherits ValidationRule
Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As System.Windows.Controls.ValidationResult
Dim bg As BindingGroup = TryCast(value, BindingGroup)
If bg IsNot Nothing AndAlso bg.Items.Count > 0 Then
'The BindingGroup Items property contains the original object(s)'
Dim fertilizerCombo As FertilizerCombination = TryCast(bg.Items(0), FertilizerCombination)
'Using the BindingGroups GetValue method to retrieve the user provided values'
Dim proposedNitrogen As Integer
Dim proposedPhosphorous As Integer
Dim proposedPotassium As Integer
Try
proposedNitrogen = bg.GetValue(fertilizerCombo, "Nitrogen")
proposedPhosphorous = bg.GetValue(fertilizerCombo, "Phosphorous")
proposedPotassium = bg.GetValue(fertilizerCombo, "Potassium")
If FertilizerCombination.FertilizerCombinationAvailable(proposedNitrogen, proposedPhosphorous, proposedPotassium) = False Then
Return New ValidationResult(False, "This fertializer combination has already been used")
End If
Catch noValue As System.Windows.Data.ValueUnavailableException
'a binding was not properly bound yet'
End Try
End If
Return New ValidationResult(True, Nothing)
End Function
End Class
I have a "PlantSolution" class that has a FertilizerCombination property. I also have a VM for that class so that it's easy for binding in the XAML:
Public Class PlantSolutionVM
Public Property PlantSolution As PlantSolution
Public Sub New()
PlantSolution = New PlantSolution("Produce Blooms", "Using this fertilizer will help your plant produce flowers!", 10, 10, 20)
End Sub
End Class
Public Class PlantSolution
Private _name As String
Private _description As String
Private _fertilizer As FertilizerCombination
<System.ComponentModel.DataAnnotations.Required()>
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
System.ComponentModel.DataAnnotations.Validator.ValidateProperty(value, NameValidationContext)
_name = value
End Set
End Property
<System.ComponentModel.DataAnnotations.Required()>
Public Property Description As String
Get
Return _description
End Get
Set(value As String)
System.ComponentModel.DataAnnotations.Validator.ValidateProperty(value, DescriptionValidationContext)
_description = value
End Set
End Property
Public ReadOnly Property Fertilizer As FertilizerCombination
Get
Return _fertilizer
End Get
End Property
Public Sub New(name As String, description As String, nitrogen As Integer, phosphorous As Integer, potassium As Integer)
_fertilizer = New FertilizerCombination(nitrogen, phosphorous, potassium)
Me.Name = name
Me.Description = description
End Sub
Private NameValidationContext = New System.ComponentModel.DataAnnotations.ValidationContext(Me, Nothing, Nothing) With {.MemberName = "Name"}
Private DescriptionValidationContext = New System.ComponentModel.DataAnnotations.ValidationContext(Me, Nothing, Nothing) With {.MemberName = "Description"}
End Class
Now here is my XAML for a window called "FunWithFocus":
<Grid>
<Grid.Resources>
<x:Array x:Key="CombinationOptions" Type="sys:Int32" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:Int32>0</sys:Int32>
<sys:Int32>1</sys:Int32>
<sys:Int32>2</sys:Int32>
<sys:Int32>3</sys:Int32>
<sys:Int32>4</sys:Int32>
<sys:Int32>5</sys:Int32>
<sys:Int32>6</sys:Int32>
<sys:Int32>7</sys:Int32>
<sys:Int32>8</sys:Int32>
<sys:Int32>9</sys:Int32>
<sys:Int32>10</sys:Int32>
<sys:Int32>11</sys:Int32>
<sys:Int32>12</sys:Int32>
<sys:Int32>13</sys:Int32>
<sys:Int32>14</sys:Int32>
<sys:Int32>15</sys:Int32>
<sys:Int32>16</sys:Int32>
<sys:Int32>17</sys:Int32>
<sys:Int32>18</sys:Int32>
<sys:Int32>19</sys:Int32>
<sys:Int32>20</sys:Int32>
</x:Array>
<local:PlantSolutionVM x:Key="PlantSolutionVM" />
<Style TargetType="DockPanel">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Grid DataContext="{Binding Source={StaticResource PlantSolutionVM}, Path=PlantSolution}" Margin="50">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Solution Name:" Grid.Row="0" Grid.Column="0" Margin="5"/>
<TextBox x:Name="ItemName" Text="{Binding Name, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" Margin="5"/>
<TextBlock Text="Description of Problem:" Grid.Row="1" Grid.Column="0" Margin="5"/>
<TextBox x:Name="ItemDescription" Text="{Binding Description, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Grid.Row="1" Grid.Column="1" Margin="5"/>
<TextBlock Text="Recommended Fertilizer:" Grid.Row="2" Grid.Column="0" Margin="5"/>
<DockPanel x:Name="FertilizerCombinationContainer" DataContext="{Binding Fertilizer}" Grid.Row="2" Grid.Column="1" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top">
<DockPanel.BindingGroup>
<BindingGroup NotifyOnValidationError="True">
<BindingGroup.ValidationRules>
<local:FertilizerCombinationValidationRule />
</BindingGroup.ValidationRules>
</BindingGroup>
</DockPanel.BindingGroup>
<ComboBox x:Name="NitrogenValue" HorizontalAlignment="Left" VerticalAlignment="Top"
ItemsSource="{StaticResource CombinationOptions}"
SelectedItem="{Binding Nitrogen}"/>
<ComboBox x:Name="PhosphorousValue" HorizontalAlignment="Left" VerticalAlignment="Top"
ItemsSource="{StaticResource CombinationOptions}"
SelectedItem="{Binding Phosphorous}"/>
<ComboBox x:Name="PotatssiumValue" HorizontalAlignment="Left" VerticalAlignment="Top"
ItemsSource="{StaticResource CombinationOptions}"
SelectedItem="{Binding Potassium}"/>
</DockPanel>
<Button x:Name="SaveIt" Content="Save" Grid.Row="3" Grid.Column="1"/>
</Grid>
</Grid>
And here is the Code Behind for the page:
Class FunWithFocus
Private Sub FertilizerCombinationContainer_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles FertilizerCombinationContainer.Loaded
'FertilizerCombinationContainer.BindingGroup.CancelEdit()'
'FertilizerCombinationContainer.BindingGroup.BeginEdit()'
End Sub
Private Sub FertilizerCombinationContainer_LostFocus(sender As Object, e As System.Windows.RoutedEventArgs) Handles FertilizerCombinationContainer.LostFocus
'This will get fired if the user drops down one of the comboboxes'
End Sub
'This is how I am currently handling the lost focus...but it doesn't fire if the user clicks somewhere that doesn't take keyboard focus'
'Private Sub FertilizerCombinationContainer_IsKeyboardFocusWithinChanged(sender As Object, e As System.Windows.DependencyPropertyChangedEventArgs) Handles FertilizerCombinationContainer.IsKeyboardFocusWithinChanged'
' If FertilizerCombinationContainer.IsKeyboardFocusWithin = False Then'
' FertilizerCombinationContainer.BindingGroup.ValidateWithoutUpdate()'
' If Validation.GetErrors(FertilizerCombinationContainer).Count = 0 Then'
' FertilizerCombinationContainer.BindingGroup.CommitEdit()'
' Dim bg As BindingGroup = FertilizerCombinationContainer.BindingGroup'
' Dim fertilizerCombo As FertilizerCombination = bg.Items(0)'
' Dim proposedNitrogen As Integer'
' Dim proposedPhosphorous As Integer'
' Dim proposedPotassium As Integer'
' Try'
' proposedNitrogen = bg.GetValue(fertilizerCombo, "Nitrogen")'
' proposedPhosphorous = bg.GetValue(fertilizerCombo, "Phosphorous")'
' proposedPotassium = bg.GetValue(fertilizerCombo, "Potassium")'
' ''there was a change: set it'
' fertilizerCombo.SetFertilizerCombination(proposedNitrogen, proposedPhosphorous, proposedPotassium)'
' Catch noValue As System.Windows.Data.ValueUnavailableException'
' ''a binding was not properly bound yet'
' End Try'
' End If'
' End If'
'End Sub'
End Class
If you put a break point on the method that handles FertilizerCombinationContainer's LostFocus event, you will notice that it is fired when you select one of the ComboBoxes even though it is a child of the FertilizerContainer element.
Thank you,
-Frinny
I've been hunting through stackoverflow for a while to answer this.
I've got a Listview who's items are Listviews whose children are actually a list(of string) that is a member of the parent listviewitem.
Drag and drop functionality is the goal. However this is proving hard for a variety of reasons, one of which is casting. I need to get the type before I do a direct cast to make it work - at least I think that will get me over one problem.
However I can't get this syntax to even begin to work, so I'll start here:
Dim itemType = listView.ItemContainerGenerator.ItemFromContainer(listViewItem)
Dim g As Type = GetType(itemtype)
This is the entire drag n drop implementation I'm trying:
Dim startpoint As Point
Public Sub List_PreviewMouseLeftButtonDown(sender As Object, e As MouseEventArgs)
' Store the mouse position
startpoint = e.GetPosition(Nothing)
End Sub
Private Sub List_MouseMove(sender As Object, e As MouseEventArgs)
Dim mousePos As Point = e.GetPosition(Nothing)
Dim diff As Vector = startpoint - mousePos
If e.LeftButton = MouseButtonState.Pressed And Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance Or Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance Then
Dim listView As ListView = DirectCast(sender, ListView)
Dim listViewItem As ListViewItem = FindAncestor(Of ListViewItem)(DirectCast(e.OriginalSource, DependencyObject))
Dim itemType = listView.ItemContainerGenerator.ItemFromContainer(listViewItem)
Dim g As Type = GetType(itemtype)
Dim item As String = DirectCast(listView.ItemContainerGenerator.ItemFromContainer(listViewItem), String)
Dim dragData As New DataObject("myFormat", item)
DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move)
End If
End Sub
Private Shared Function FindAncestor(Of T As DependencyObject)(current As DependencyObject) As T
Do
If TypeOf current Is T Then
Return DirectCast(current, T)
End If
current = VisualTreeHelper.GetParent(current)
Loop While current IsNot Nothing
Return Nothing
End Function
Private Sub DropList_DragEnter(sender As Object, e As DragEventArgs)
If Not e.Data.GetDataPresent("myFormat") OrElse sender = e.Source Then
e.Effects = DragDropEffects.None
End If
End Sub
Private Sub DropList_Drop(sender As Object, e As DragEventArgs)
If e.Data.GetDataPresent("myFormat") Then
Dim contact As String = TryCast(e.Data.GetData("myFormat"), String)
Dim listView As ListView = TryCast(sender, ListView)
listView.Items.Add(contact)
End If
End Sub
Here is the nested listView:
<!--DataContext="{StaticResource RcpdInsertViewSource}" This is a collectionviewsource.
RCPDInsert has a list(of string) member that is created from a single string property
and whose order needs to be alterable.
Eg rcpdInsert.template="[stuff] [more stuff]" so rcpdInsert.templateList = list(of String) from template.split("] [") -->
<ListView Grid.Column="1" Grid.Row="1" ItemsSource="{Binding}"
PreviewMouseLeftButtonDown="List_PreviewMouseLeftButtonDown"
PreviewMouseMove="List_MouseMove"
Drop="DropList_Drop"
DragEnter="DropList_DragEnter"
AllowDrop="True">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBox Text="{Binding Path=cpID}"></TextBox>
<TextBox Text="{Binding Path=fieldRef}"></TextBox>
<ListView ItemsSource="{Binding Path=InsertsList}" >
<ListView.ItemTemplate>
<DataTemplate DataType="DataClasses1:RcpdInsert.template" >
<StackPanel Orientation="Horizontal" Grid.Row="0">
<TextBlock Text="{Binding}" Margin="5" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Goal: Drag and drop reordering of child listviews, ideally being able to pull individual items from one child listView to another. When saved, the code behind will actually concat the strings back together and update the template member.
For context here are the relevant members of RcpdInsert:
Sub SetupInsertList()
_insertsList = template.Split(" ").ToList()
For Each item In InsertsList
Dim t = item
RcpdList.Add(RcpdSet.RpcdListShared.Where(Function(x) x.insertID = t).ToList())
Next
End Sub
Public Property RcpdList As New List(Of List(Of Rcpd))
Private Property _insertsList As New List(Of String)
Public Property InsertsList As List(Of String)
Get
If _insertsList.Count = 0 Then setupInsertList()
Return _insertsList
End Get
Set(value As List(Of String))
Dim combine As String = value.Aggregate("", Function(current, i) current + (i & " "))
template = combine
End Set
End Property
The casting is one issue with this, I'm hoping being able to do this part means that the others will be easier to resolve.
Thanks in advance to anyone who can help :)
Is it possible to rearrange tab items in tab control in run time? For example I have 3 tab items which are about cars and 4 tabs about house. I want to be able to rearrange them using drag and drop. Is it possible or it is something fantastic?
I have Tab Control here is XAML.
<TabControl x:Name="tc" Visibility="Collapsed" GotFocus="Focus" AllowDrop="True" >
</TabControl>
Tab items will be added in runtime.
Thanks for helping me!
found a solution in the MSDN forum.
Here is the link:
DragDrop TabItem
Here is the solution:
C# solution
WPF code:
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="AllowDrop" Value="True"/>
<EventSetter Event="PreviewMouseMove" Handler="TabItem_PreviewMouseMove"/>
<EventSetter Event="Drop" Handler="TabItem_Drop"/>
</Style>
</TabControl.Resources>
<TabItem Header="Tabitem 1"/>
<TabItem Header="Tabitem 2"/>
<TabItem Header="Tabitem 3"/>
<TabItem Header="Tabitem 4"/>
<TabItem Header="Tabitem 5"/>
</TabControl>
C# code behind:
private void TabItem_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (!(e.Source is TabItem tabItem))
{
return;
}
if (Mouse.PrimaryDevice.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(tabItem, tabItem, DragDropEffects.All);
}
}
private void TabItem_Drop(object sender, DragEventArgs e)
{
if (e.Source is TabItem tabItemTarget &&
e.Data.GetData(typeof(TabItem)) is TabItem tabItemSource &&
!tabItemTarget.Equals(tabItemSource) &&
tabItemTarget.Parent is TabControl tabControl)
{
int targetIndex = tabControl.Items.IndexOf(tabItemTarget);
tabControl.Items.Remove(tabItemSource);
tabControl.Items.Insert(targetIndex, tabItemSource);
tabItemSource.IsSelected = true;
}
}
When I tried to implement this solution, the drop event was firing twice (moving the tabs but them immediately moving them back). I had to add an integer to keep track of the last tab target index. My solution is in VB.NET
'additional variable
Dim lastTabTargetIndex As Integer = Nothing
Private Sub tc1_PreviewMouseMove(sender As Object, e As MouseEventArgs) Handles tc1.PreviewMouseMove
Dim Tabi = TryCast(e.Source, TabItem)
If Tabi Is Nothing Then
Exit Sub
Else
If Mouse.PrimaryDevice.LeftButton = MouseButtonState.Pressed Then
DragDrop.DoDragDrop(Tabi, Tabi, DragDropEffects.All)
End If
End If
End Sub
Private Sub tc1_Drop(sender As Object, e As DragEventArgs) Handles tc1.Drop
Dim tabItemTarget = TryCast(e.Source, TabItem)
Dim tabItemSource = TryCast(e.Data.GetData(GetType(TabItem)), TabItem)
If Not tabItemTarget.Equals(tabItemSource) Then
Dim tabControl = TryCast(tabItemTarget.Parent, TabControl)
Dim sourceIndex As Integer = tabControl.Items.IndexOf(tabItemSource)
Dim targetIndex As Integer = tabControl.Items.IndexOf(tabItemTarget)
'had to use this extra statement
If sourceIndex <> lastTabTargetIndex Then
'assign lastTabTargetIndex here
lastTabTargetIndex = targetIndex
tabControl.Items.Remove(tabItemSource)
tabControl.Items.Insert(targetIndex, tabItemSource)
tabControl.Items.Remove(tabItemTarget)
tabControl.Items.Insert(sourceIndex, tabItemTarget)
End If
End If
End Sub