I have a RadTreeView (treeview) and a RadGridView (gridview). The user should be able to move elements from the gridview to the treeview and change the effect of the drag&drop action by using the shift or control key (like in Windows Explorer).
In the GiveFeedback event the effect is set according to the pressed key and also the visualization shows the expected behavior. But in the Drop event (on the treeview) and in the DragDropCompleted event (on the gridview) the effect is set to All, therefore I'm not able to decide if I have to remove the Element from the current list.
Am I missing something?
XAML:
<telerik:RadTreeView AllowDrop="True"
ItemsSource="{Binding Folders}"
SelectedItem="{Binding SelectedFolder,
Mode=TwoWay}"
SelectionMode="Single">
<telerik:RadTreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding Children}">
<Grid VerticalAlignment="Center">
<TextBlock VerticalAlignment="Center"
Text="{Binding Name}"
local:TreeItemDropBehavior.IsEnabled="True" />
</Grid>
</HierarchicalDataTemplate>
</telerik:RadTreeView.Resources>
</telerik:RadTreeView>
<telerik:RadGridView Grid.Column="1"
AllowDrop="True"
AutoGenerateColumns="False"
CanUserFreezeColumns="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
FontSize="12"
IsFilteringAllowed="False"
IsReadOnly="True"
ItemsSource="{Binding SelectedFolder.Elements}"
RowHeight="32"
RowIndicatorVisibility="Collapsed"
SelectionMode="Multiple"
ShowGroupPanel="False"
local:GridViewDragDropBehavior.IsEnabled="True"
telerik:DragDropManager.AllowCapturedDrag="True"
telerik:DragDropManager.AllowDrag="True">
<telerik:RadGridView.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<DataTemplate x:Key="DraggedItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Dragging: " />
<TextBlock FontWeight="Bold" Text="{Binding CurrentDraggedItems.Count}" />
<TextBlock Text=" Element(s)" />
</StackPanel>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</telerik:RadGridView.Resources>
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Width="100"
DataMemberBinding="{Binding Name}"
Header="Name" />
<telerik:GridViewDataColumn Width="250"
DataMemberBinding="{Binding Description}"
Header="Description" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
GridViewDragDropBehavior:
public class GridViewDragDropBehavior
{
public RadGridView AssociatedControl { get; set; }
private void SubscribeToDragDropEvents()
{
DragDropManager.AddDragInitializeHandler(AssociatedControl, OnDragInitialize);
DragDropManager.AddGiveFeedbackHandler(AssociatedControl, OnGiveFeedback);
DragDropManager.AddDragDropCompletedHandler(AssociatedControl, OnDragDropCompleted);
DragDropManager.AddDragOverHandler(AssociatedControl, OnDragOver);
}
private void UnsubscribeFromDragDropEvents()
{
DragDropManager.RemoveDragInitializeHandler(AssociatedControl, OnDragInitialize);
DragDropManager.RemoveGiveFeedbackHandler(AssociatedControl, OnGiveFeedback);
DragDropManager.RemoveDragDropCompletedHandler(AssociatedControl, OnDragDropCompleted);
DragDropManager.RemoveDragOverHandler(AssociatedControl, OnDragOver);
}
private void OnDragInitialize(object sender, DragInitializeEventArgs e)
{
DropIndicationDetails details = new DropIndicationDetails();
var gridView = sender as RadGridView;
details.DragSource = gridView.ItemsSource;
var items = gridView.SelectedItems;
details.CurrentDraggedItems = items;
IDragPayload dragPayload = DragDropPayloadManager.GeneratePayload(null);
dragPayload.SetData("DraggedData", items);
dragPayload.SetData("DropDetails", details);
e.Data = dragPayload;
e.DragVisual = new DragVisual { Content = details, ContentTemplate = AssociatedControl.Resources["DraggedItemTemplate"] as DataTemplate };
e.DragVisualOffset = new Point(e.RelativeStartPoint.X + 10, e.RelativeStartPoint.Y);
e.AllowedEffects = DragDropEffects.All;
}
private void OnGiveFeedback(object sender, GiveFeedbackEventArgs e)
{
Debug.WriteLine("GridViewDragDropBehavior.OnGiveFeedback {0}", e.Effects);
e.SetCursor(Cursors.Arrow);
e.Handled = true;
}
private void OnDragDropCompleted(object sender, DragDropCompletedEventArgs e)
{
Debug.WriteLine("GridViewDragDropBehavior.OnDragDropCompleted: {0}", e.Effects);
var data = DragDropPayloadManager.GetDataFromObject(e.Data, "DraggedData") as IList;
var details = DragDropPayloadManager.GetDataFromObject(e.Data, "DropDetails");
Debug.WriteLine(e.Effects);
// Remove Element from source list if drag drop effect is move
/*if (e.Effects == DragDropEffects.Move)
{
var collection = (details as DropIndicationDetails).DragSource as IList;
foreach(var element in data)
{
collection.Remove(element);
}
}*/
}
private void OnDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
Debug.WriteLine("GridViewDragDropBehavior.OnDragOver: {0}", e.Effects);
e.Effects = DragDropEffects.None;
e.Handled = true;
}
}
TreeItemDropBehavior.cs:
public class TreeItemDropBehavior
{
protected virtual void Initialize()
{
DragDropManager.AddGiveFeedbackHandler(AssociatedObject, OnGiveFeedback);
DragDropManager.AddDropHandler(AssociatedObject, OnDrop);
}
protected virtual void CleanUp()
{
DragDropManager.RemoveGiveFeedbackHandler(AssociatedObject, OnGiveFeedback);
DragDropManager.RemoveDropHandler(AssociatedObject, OnDrop);
}
private void OnGiveFeedback(object sender, Telerik.Windows.DragDrop.GiveFeedbackEventArgs e)
{
Debug.WriteLine("TreeItemDropBehavior.OnGiveFeedback {0}", e.Effects);
e.SetCursor(Cursors.Arrow);
e.Handled = true;
}
private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
Debug.WriteLine("TreeItemDropBehavior.OnDrop: {0}", e.Effects);
if (e.Effects != DragDropEffects.None)
{
var destinationItem = (e.OriginalSource as FrameworkElement).ParentOfType<RadTreeViewItem>();
var data = DragDropPayloadManager.GetDataFromObject(e.Data, "DraggedData") as IList;
var details = DragDropPayloadManager.GetDataFromObject(e.Data, "DropDetails") as DropIndicationDetails;
if (destinationItem != null)
{
foreach (var element in data)
{
(destinationItem.DataContext as Folder).Elements.Add(element as Element);
}
e.Handled = true;
}
}
}
}
Related
I would like to create an horizontal dynamic listbox:
A button is visible when the mouse is between two items.
<ListBox
MinHeight="32"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
SelectionMode="Extended"
HorizontalAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>
<TextBlock><Run Text="C1" /></TextBlock>
</ListBoxItem>
<ListBoxItem>
<TextBlock><Run Text="C2" /></TextBlock>
</ListBoxItem>
<ListBoxItem>
<TextBlock><Run Text="C3" /></TextBlock>
</ListBoxItem>
<ListBoxItem>
<TextBlock><Run Text="C4" /></TextBlock>
</ListBoxItem>
</ListBox>
</ListBox>
Any suggestions, please?
Thank you
EDIT
private void myElement_MouseEnter(object sender, MouseEventArgs e)
{
//change button visibility
if (sender is Grid item)
{
if (item.DataContext is DataModel data)
{
System.Diagnostics.Debug.WriteLine("myElement_MouseEnter: " + data.TextValue);
}
var border1 = (Border)item.FindName("HitTestBorder1");
var border2 = (Border)item.FindName("HitTestBorder2");
if (border1 is Border)
{
var margin = border1.Margin;
margin.Left = -item.ActualWidth;
border1.Margin = margin;
}
if (border2 is Border)
{
var margin = border2.Margin;
margin.Left = item.ActualWidth;
border2.Margin = margin;
}
}
}
The problem is that the grid width is resized after mouse_enter... So, I don't get the overlay "effect".
Here is the solution.
I created a canvas. It contains 2 borders with button inside and ZPanel is setted very high (=1000) to be on top of everything.
I change the position of these borders on MouseEnter and MouseLeave events of ListBoxItem (see ItemTemplate).
XAML
<Border Padding="10">
<Canvas x:Name="supergrid" MouseLeave="supergrid_MouseLeave">
<Border x:Name="HitTestBorder1"
Panel.ZIndex="1000"
Background="Transparent"
BorderBrush="Transparent"
HorizontalAlignment="Center"
VerticalAlignment="Center"
BorderThickness="0,0,0,0" >
<Button x:Name="button1"
Visibility="Hidden"
Content="+"
Height="18" Width="18" FontSize="6" Background="Red"
Click="button1_Click"
/>
</Border>
<Border x:Name="HitTestBorder2"
Panel.ZIndex="1000"
Background="Transparent"
BorderBrush="Transparent"
HorizontalAlignment="Center"
VerticalAlignment="Center"
BorderThickness="1,1,1,1" >
<Button x:Name="button2"
Visibility="Hidden"
Content="+"
Height="18" Width="18" FontSize="6" Background="Green"
Click="button2_Click"
/>
</Border>
<ListBox x:Name="HorizontalListBox"
ItemsSource="{Binding DataModels}"
Margin="0,0,0,0"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Background="Yellow" MouseLeave="HorizontalListBox_MouseLeave">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<!--<Setter Property="MinWidth" Value="60" />
<Setter Property="MinHeight" Value="40" />-->
<Setter Property="Background" Value="Blue" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="myElement"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
MouseEnter="myElement_MouseEnter"
MouseLeave="myElement_MouseLeave"
Background="White">
<TextBlock x:Name="myText"
Margin="10"
Text="{Binding TextValue}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextAlignment="Center" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Canvas>
</Border>
C#
public partial class MainWindow : Window
{
private DataModel currentDataModel;
public ObservableCollection<DataModel> DataModels { get; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.DataModels = new ObservableCollection<DataModel>();
this.DataModels.Add(new DataModel("Item1"));
this.DataModels.Add(new DataModel("Item2"));
this.DataModels.Add(new DataModel("SuperMegaHyperLong"));
this.DataModels.Add(new DataModel("Item3"));
this.DataModels.Add(new DataModel("Item4"));
this.DataModels.Add(new DataModel("123"));
this.DataModels.Add(new DataModel("Item5"));
}
private void myElement_MouseEnter(object sender, MouseEventArgs e)
{
updateOverlay((Grid)sender);
}
private void updateOverlay(Grid lbi)
{
if (lbi is Grid item) //the grid of listboxitem
{
if (item.DataContext is DataModel data)
{
Debug.WriteLine("myElement_MouseEnter: " + data.TextValue);
}
var myText = (TextBlock)item.FindName("myText");
if (myText is TextBlock)
{
Point relativePoint = myText.TransformToAncestor(supergrid)
.Transform(new Point(0, 0));
Debug.WriteLine("relativePoint: " + relativePoint.ToString());
//update left button position
double w = button1.ActualWidth;
double h = button1.ActualHeight;
double x = relativePoint.X - w / 2.0 - myText.Margin.Left;
double y = relativePoint.Y;
updateMargin(HitTestBorder1,
x,
x + w,
y + h,
y);
//update right button position
w = button2.ActualWidth;
h = button2.ActualHeight;
x = relativePoint.X - w / 2.0 - myText.Margin.Left;
x += item.ActualWidth;
y = relativePoint.Y;
updateMargin(HitTestBorder2,
x,
x + w,
y + h,
y);
//show the button
button1.Visibility = button2.Visibility = Visibility.Visible;
//the current item
if (myText.DataContext is DataModel dm)
{
this.currentDataModel = dm;
}
}
}
}
private void updateMargin(Border border, double left, double right, double bottom, double top)
{
//border = HitTestBorder2;
var margin = border.Margin;
margin.Left = left;
margin.Right = right;
margin.Top = top;
margin.Bottom = bottom;
border.Margin = margin;
Debug.WriteLine("updateMargin Left: " + left.ToString() + "Right: " + right.ToString());
}
private void myElement_MouseLeave(object sender, MouseEventArgs e)
{
//change button visibility
if (sender is Grid item)
{
if (item.DataContext is DataModel data)
{
System.Diagnostics.Debug.WriteLine("myElement_MouseLeave: " + data.TextValue);
}
//button1.Visibility = button2.Visibility = Visibility.Hidden;
}
}
private void HorizontalListBox_MouseLeave(object sender, MouseEventArgs e)
{
Debug.WriteLine("HorizontalListBox_MouseLeave");
//button1.Visibility = button2.Visibility = Visibility.Hidden;
}
private void supergrid_MouseLeave(object sender, MouseEventArgs e)
{
Debug.WriteLine("supergrid_MouseLeave: ");
button1.Visibility = button2.Visibility = Visibility.Hidden;
// => :D
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("LEFT INSERT");
int idx = this.DataModels.IndexOf(currentDataModel);
DataModel newDataModel = new DataModel($"Item{this.DataModels.Count}");
this.DataModels.Insert(idx, newDataModel);
button1.Visibility = button2.Visibility = Visibility.Hidden;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("RIGHT INSERT");
int idx = this.DataModels.IndexOf(currentDataModel);
this.DataModels.Insert(idx+1, new DataModel($"Item{this.DataModels.Count}"));
button1.Visibility = button2.Visibility = Visibility.Hidden;
}
}
public class DataModel
{
public string TextValue { get; set; }
public DataModel(string textValue)
{
this.TextValue = textValue;
}
}
As you can see, there is place for improvements (like select the new ListBoxItem). Disable the buttons when we drag, animation, delay, etc.
i want to get in my codebehind the Content of an button that has a grid in it with multiple textboxes.
i had this before Code and this works:
XAML:
<Button Click="btnClick_upload_Data">
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="test1" ></TextBlock>
<TextBlock Text="test2" ></TextBlock>
</StackPanel>
</Button.Content>
</Button>
codebehind:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
string s = ((((sender as Button).Content) as StackPanel).Children[1] as TextBlock).Text;
//…
and this way i got the "test2" im my string variable.
now my XAML has changed a bit
my Question is how do i have to Change my function so i still get "test2" in my string variable 's'
new XAML:
<Button Click="btnClick_upload_Data" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="test1" Grid.Row="0"></TextBlock>
<TextBlock Text="test2" Grid.Row="1"></TextBlock>
</Grid>
</StackPanel>
</Button.Content>
</Button>
new XAML:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
//????
thanks in advance
Try this:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
string s = null;
Button btn = (Button)sender;
StackPanel sp = btn.Content as StackPanel;
if (sp != null && sp.Children.Count > 0)
{
Grid grid = sp.Children[0] as Grid;
if (grid != null && grid.Children.Count > 1)
{
TextBlock textBlock = grid.Children[1] as TextBlock;
if (textBlock != null)
s = textBlock.Text;
}
}
MessageBox.Show(s);
}
I am creating UWP app, and I maked external arrow "marker" of selected item in listview...
Like this:
I have managed to achieve this with next code:
var current = lvMain.Items.FirstOrDefault(a => (a as MyModel).Selected) as MyModel;
ListViewItem selected = lvMain.ContainerFromItem(current) as ListViewItem;
GeneralTransform generalTransform1 = gvEpg.TransformToVisual(selected);
Point currentPoint = generalTransform1.TransformPoint(new Point());
In Scroll change event I am calling this and set the arrow position by the Point of my item. And this is working.
But, I want to simplified this. Is there any kind of binding or something like that, that would make arrow always follow the item?
Here's the sample.
XAML MainPage:
<Page.Resources>
<DataTemplate x:Key="DataTemplate">
<Canvas Height="80" Width="200">
<TextBlock Text="{Binding}"/>
</Canvas>
</DataTemplate>
</Page.Resources>
<StackPanel Orientation="Horizontal">
<ListView x:Name="ListView" Width="400"
SelectionChanged="ListView_OnSelectionChanged"
ItemTemplate="{StaticResource DataTemplate}"/>
<Canvas x:Name="ParentCanvas">
<Image x:Name="Arrow"
Stretch="UniformToFill" Width="200" Height="80"
Source="Assets/Red_Left_Arrow.png"/>
</Canvas>
</StackPanel>
Code behind:
private readonly List<string> _names = new List<string>();
private Visual _rectangleVisual;
private Visual _parentVisual;
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 32; i++)
{
_names.Add("item " + i);
}
ListView.ItemsSource = _names;
_parentVisual = ElementCompositionPreview.GetElementVisual(ParentCanvas);
_rectangleVisual = ElementCompositionPreview.GetElementVisual(Arrow);
var border = VisualTreeHelper.GetChild(ListView, 0) as Border;
var scrollViewer = border.Child as ScrollViewer;
var scrollerProperties = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(scrollViewer);
var offsetExpressionAnimation = _rectangleVisual.Compositor.CreateExpressionAnimation("Scroller.Translation.Y");
offsetExpressionAnimation.SetReferenceParameter("Scroller", scrollerProperties);
_rectangleVisual.StartAnimation("Offset.Y", offsetExpressionAnimation);
}
private void ListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listViewItem = ListView.ContainerFromItem(ListView.SelectedItem) as ListViewItem;
var listItemVisual = ElementCompositionPreview.GetElementVisual(listViewItem);
_parentVisual.Offset = new Vector3(_parentVisual.Offset.X, listItemVisual.Offset.Y, 0);
}
Looks like what you asked for:
I'm trying to sort a ListView by it's headers.
I'm following this MSDN example, with the alternation given here - where this line works if I were to use direct binding:
GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
string sortString = ((Binding)headerClicked.Column.DisplayMemberBinding).Path.Path
But problem is I'm not binding the columns directly using DisplayMemberBinding="{Binding PVNum}" but rather I am using CellTemplate:
<ListView.Resources>
<DataTemplate x:Key="NumberTemplate">
<TextBlock Text="{Binding PVNum}" TextAlignment="Center" />
</DataTemplate>
</ListView.Resources>
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridView.Columns>
<GridViewColumn Header=" " CellTemplate="{StaticResource NumberTemplate}"/>
</GridView.Columns>
</GridView>
</ListView.View>
So my question is - how do I get this "PVNum" string in the code behind?
I did try this, though s is null - so I guess I'm off:
var t = headerClicked.Column.CellTemplate.LoadContent() as TextBlock;
var s = t.GetBindingExpression(TextBox.TextProperty);
Any suggestions?
It should be TextBlock.Text property:
var t = headerClicked.Column.CellTemplate.LoadContent() as TextBlock;
var s = t.GetBindingExpression(TextBlock.TextProperty);
string sourcePropertyName = s.ParentBinding.Path.Path;
A possible solution is to define an attached property for GridViewColumn:
public class GridViewColumnAttachedProperties
{
public static readonly DependencyProperty SortPropertyNameProperty = DependencyProperty.RegisterAttached(
"SortPropertyName", typeof(string), typeof(GridViewColumnAttachedProperties), new PropertyMetadata(default(string)));
public static void SetSortPropertyName(DependencyObject element, string value)
{
element.SetValue(SortPropertyNameProperty, value);
}
public static string GetSortPropertyName(DependencyObject element)
{
return (string) element.GetValue(SortPropertyNameProperty);
}
}
In XAML you set the attached properties to the Path used in the Binding inside the templates. Based on the example from the MSDN site:
<ListView x:Name='lv' Height="150" HorizontalAlignment="Center" VerticalAlignment="Center" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
<ListView.Resources>
<DataTemplate x:Key="YearTemplate">
<TextBlock Text="{Binding Year}" TextAlignment="Center" />
</DataTemplate>
<DataTemplate x:Key="MonthTemplate">
<TextBlock Text="{Binding Month}" TextAlignment="Center" />
</DataTemplate>
<DataTemplate x:Key="DayTemplate">
<TextBlock Text="{Binding Day}" TextAlignment="Center" />
</DataTemplate>
</ListView.Resources>
<ListView.ItemsSource>
<collections:ArrayList>
<system:DateTime>1993/1/1 12:22:02</system:DateTime>
<system:DateTime>1993/1/2 13:2:01</system:DateTime>
<system:DateTime>1997/1/3 2:1:6</system:DateTime>
<system:DateTime>1997/1/4 13:6:55</system:DateTime>
<system:DateTime>1999/2/1 12:22:02</system:DateTime>
<system:DateTime>1998/2/2 13:2:01</system:DateTime>
<system:DateTime>2000/2/3 2:1:6</system:DateTime>
<system:DateTime>2002/2/4 13:6:55</system:DateTime>
<system:DateTime>2001/3/1 12:22:02</system:DateTime>
<system:DateTime>2006/3/2 13:2:01</system:DateTime>
<system:DateTime>2004/3/3 2:1:6</system:DateTime>
<system:DateTime>2004/3/4 13:6:55</system:DateTime>
</collections:ArrayList>
</ListView.ItemsSource>
<ListView.View>
<GridView>
<GridViewColumn CellTemplate="{StaticResource YearTemplate}" local:GridViewColumnAttachedProperties.SortPropertyName="Year" />
<GridViewColumn CellTemplate="{StaticResource MonthTemplate}" local:GridViewColumnAttachedProperties.SortPropertyName="Month" />
<GridViewColumn CellTemplate="{StaticResource DayTemplate}" local:GridViewColumnAttachedProperties.SortPropertyName="Day" />
</GridView>
</ListView.View>
</ListView>
And in the Click event handler you can just retrieve the value of the attached property with string bindingName = headerClicked.Column.GetValue(GridViewColumnAttachedProperties.SortPropertyNameProperty) as string;. Based on the MSDN example:
GridViewColumnHeader _lastHeaderClicked;
ListSortDirection _lastDirection = ListSortDirection.Ascending;
private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
{
GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
if (headerClicked != null)
{
if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
{
ListSortDirection direction;
if (!ReferenceEquals(headerClicked, _lastHeaderClicked))
{
direction = ListSortDirection.Ascending;
}
else
{
if (_lastDirection == ListSortDirection.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
direction = ListSortDirection.Ascending;
}
}
string bindingName = headerClicked.Column.GetValue(GridViewColumnAttachedProperties.SortPropertyNameProperty) as string;
Sort(bindingName, direction);
if (direction == ListSortDirection.Ascending)
{
headerClicked.Column.HeaderTemplate = Resources["HeaderTemplateArrowUp"] as DataTemplate;
}
else
{
headerClicked.Column.HeaderTemplate = Resources["HeaderTemplateArrowDown"] as DataTemplate;
}
// Remove arrow from previously sorted header
if (_lastHeaderClicked != null && !ReferenceEquals(_lastHeaderClicked, headerClicked))
{
_lastHeaderClicked.Column.HeaderTemplate = null;
}
_lastHeaderClicked = headerClicked;
_lastDirection = direction;
}
}
}
private void Sort(string sortBy, ListSortDirection direction)
{
ICollectionView dataView = CollectionViewSource.GetDefaultView(lv.ItemsSource);
dataView.SortDescriptions.Clear();
SortDescription sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}
Good day.
Have a DataGrid (operationGrid) RowDetailsTemplate, which contains the DataGrid (sumOperationGrid)
<DataGrid AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="True" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=OperCountUnits}" Name="operationGrid" RowDetailsVisibilityMode="VisibleWhenSelected" SelectionMode="Single" VerticalContentAlignment="Bottom" Grid.ColumnSpan="2" Margin="6,60,7,6" LoadingRowDetails="operationGrid_LoadingRowDetails" SelectionChanged="operationGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Id" Binding="{Binding Path=Id}" Header="{Binding NumberOfOperation, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="StartedAt" Binding="{Binding Path=StartedAt, Converter={StaticResource ConvertCultureDate}}" Header="{Binding StartedAt, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="FinisedAt" Binding="{Binding Path=FinishedAt, Converter={StaticResource ConvertCultureDate}}" Header="{Binding FinishedAt, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="CountMode" Binding="{Binding Path=Description}" Header="{Binding CountMode, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="CurrencyCode" Header="{Binding CurrcencyCode, Source={StaticResource resources}}" IsReadOnly="True" />
<DataGridTextColumn x:Name="UnitId" Binding="{Binding Path=UnitId}" Header="UnitId" IsReadOnly="True" Visibility="Collapsed" />
<DataGridTextColumn x:Name="CountModeId" Binding="{Binding Path=CountModeId}" Header="CountModeId" IsReadOnly="True" Visibility="Collapsed" />
<DataGridTextColumn x:Name="HotListCount" Binding="{Binding Path=HotListCount}" Header="Фальш" IsReadOnly="True" Visibility="Collapsed" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding CurrencySum}" Name="sumOperationGrid" SelectionChanged="sumOperationGrid_SelectionChanged" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding csId}" Visibility="Collapsed" />
<DataGridTextColumn Binding="{Binding CurrId}" Visibility="Collapsed" />
<DataGridTextColumn Binding="{Binding Curr}" Header="{Binding Currency, Source={StaticResource resources}}" />
<DataGridTextColumn Binding="{Binding TotalSum, Converter={StaticResource replaceNull}}" Header="{Binding Sum, Source={StaticResource resources}}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
The request is for the WCF service and it turns out the answer is everything is good
If you submit data through a delegate, then the answer is in the datagrid (sumOperationGrid) come and displays.
private delegate void CurrencySumByCurrentItemInvoke(DataGrid sumGrid,List<CurrencySum> sum );
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
DataGrid s2 = e.DetailsElement.FindName("sumOperationGrid") as DataGrid;
client.BeginGetCurrencySumByCurrentItem((int)_con.Id,
new AsyncCallback(
delegate(IAsyncResult result)
{
List<CurrencySum> sum = client.EndGetCurrencySumByCurrentItem(result).ToList<CurrencySum>();
if (sum != null)
{
s2.Dispatcher.BeginInvoke(new CurrencySumByCurrentItemInvoke(GetCurrencySumByCurrentItemInvoke), new object[2] {s2, sum});
}
}
), null);
}
void GetCurrencySumByCurrentItemInvoke(DataGrid sumGrid, List<CurrencySum> sum)
{
sumGrid.ItemsSource = sum;
}
Tried so.
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
DataGrid s2 = e.DetailsElement.FindName("sumOperationGrid") as DataGrid;
client.BeginGetCurrencySumByCurrentitem((int)_con.Id, new AsyncCallback (GetCurrencySumByCurrentItemCallBack), null);
}
private delegate void CurrencySumByCurrentItemInvoke(DataGrid sumGrid,List<CurrencySum> sum );
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
DataGridRow opGrRow = (DataGridRow)(this.operationGrid.ItemContainergenerator.containerfromitem (this.operationGrid.Items.Currentitem));
ContentPresenter cpOpGrRow = FindVisualChild<ContentPresenter>(opGrRow);
DataTemplate dtOpGrRow = cpOpGrRow.ContentTemplate;
DataGrid s2 = (DataGrid)dtOpGrRow.FindName("sumOperationGrid", cpOpGrRow);
}
void GetCurrencySumByCurrentItemInvoke(DataGrid sumGrid, List<CurrencySum> sum)
{
sumGrid.ItemsSource = sum;
}
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
and so
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
DataGrid s2 = e.DetailsElement.FindName("sumOperationGrid") as DataGrid;
client.BeginGetCurrencySumByCurrentitem((int)_con.Id, new AsyncCallback (GetCurrencySumByCurrentItemCallBack), s2);
}
private delegate void CurrencySumByCurrentItemInvoke(DataGrid sumGrid,List<CurrencySum> sum );
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
DataGrid s2 = result.AsyncState as DataGrid
List<CurrencySum> sum = client.EndGetCurrencySumByCurrentitem(result).ToList<CurrencySum>();
if (sum != null)
{
s2.Dispatcher.BeginInvoke(new CurrencySumByCurrentItemInvoke(GetCurrencySumByCurrentItemInvoke), new object[2] {s2, sum});
}
}
void GetCurrencySumByCurrentItemInvoke(DataGrid sumGrid, List<CurrencySum> sum)
{
sumGrid.ItemsSource = sum;
}
In both cases, it all boils down to the error
"The calling thread cannot access this object because the owner of this object is another thread."
The question of how to remove a delegate from client.BeginGetCurrencySumByCurrentitem and forward data table s2 in a separate function?
How do I get back into your flow and load data into a table sumOperationGrid ?
If I understood messy code I cant understand why you trying to pass through data grid everywhere. Only try to access grid and set ItemsSource at the end of operation only by passing sum. Here is pseudocode of callback function:
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
if(sum != null)
{
this.Invoke(new Action(()=>
{
//get data grid
//set ItemSource
})
}
}
Or if you do need to have data grid to Invoke try to do something like this:
DataGrid myDataGrid;
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
myDataGrid = e.DetailsElement.FindName("sumOperationGrid") as DataGrid;
//do stuff without passing grid
}
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
List<CurrencySum> sum = client.EndGetCurrencySumByCurrentitem(result).ToList<CurrencySum>();
if (sum != null)
{
myDataGrid.Dispatcher.BeginInvoke(new Action(()=>
{
myDataGrid.ItemSource = sum;
});
}
Yea. This a good idea. Thank a lot!
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
if(sum != null)
{
this.Invoke(new Action(()=>
{
//get data grid
//set ItemSource
})
}
}
I changed my code
private void operationGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
OperCountUnit _con = (OperCountUnit)e.Row.DataContext;
client.BeginGetCurrencySumByCurrentItem((int)_con.Id, new AsyncCallback(GetCurrencySumByCurrentItemCallBack), null);
}
private delegate void CurrencySumByCurrentItemInvoke(List<CurrencySum> sum);
private void GetCurrencySumByCurrentItemCallBack(IAsyncResult result)
{
List<CurrencySum> sum = client.EndGetCurrencySumByCurrentItem(result).ToList<CurrencySum>();
if (sum != null)
{
Dispatcher.Invoke(new CurrencySumByCurrentItemInvoke(GetCurrencySumByCurrentItemInvoke), new object[1] { sum });
}
}
void GetCurrencySumByCurrentItemInvoke(List<CurrencySum> sum)
{
DataGridRow opGrRow = (DataGridRow)(this.operationGrid.ItemContainerGenerator.ContainerFromItem(this.operationGrid.SelectedItem));
DataGridDetailsPresenter cpOpGrRow = FindVisualChild<DataGridDetailsPresenter>(opGrRow);
DataTemplate dtOpGrRow = cpOpGrRow.ContentTemplate;
DataGrid s2 = (DataGrid)dtOpGrRow.FindName("sumOperationGrid", cpOpGrRow);
s2.ItemsSource = sum;
}