Access value of an UIItem with Teststack.White - wpf

I want to access the Value of a cell in a data grid.
But I can not figured how. Or is there no way to access it with Teststack.White?
ListView listView = MainWin.Get<ListView>(SearchCriteria.ByAutomationId("dataGrid"));
ListViewCell cell = listView.Rows[2].Cells[6];
Assert.That(cell.Value == "2"); //But a value of the cell is not available

Related

How to assign a Row Value to datagrid wpf

How can I assign a single value to datagrid wpfenter image description here
I have added screenshot. I want to row(0) has to without value and row(1) has a value to assign. Thanks...

How to change the height of an individual cell in a datagridview?

I would like to know how you would change the height of a cell without affecting the whole row. I'm making a timetable form and I need to change the height of a cell depending on the amount of time the event takes.
I'm making this in a clr project and using C++
dataGridView1->Rows[0]->Cells[0]->Style->BackColor = Color::Red;
sets the background colour of the first cell on the first row to red.
Change dataGridView1 to whatever your datagridview is called and set the row and column values as you need them.
You will probably also have to add: - using namespace System::Drawing;
You have to use Style.BackColor instead
foreach (DataGridViewCell cell in dataGridView1.Rows[0].Cells)
cell.Style.BackColor = Color.Red;

Drawing line in different ListBoxItems of different ListBox?

Now, i have two listboxes,i want to draw a line from a listboxitem of this listbox to a listboxitem of another listbox. the two listboxitem isn't horizontal if possible. How to get listboxitem coordinate???
First you have to get both items you want to connect.
If you have both items you can start calculating the points. I would look for a parent Panel of both listboxes and calculate the points relative to that Panel.
As example you create Grid within two listboxes. Now you just have to calculate the points of both items. Now add a Line to the Grid that contains the two calculated points (point1 = x1, y1 and point2 = x2, y2).
But remember. It is not as easy as it seems because if you scroll you have to update the points. And exactly at that point the next problem apears. If you scroll out view (the items you want to connect) the line will be still visible. So you have to calculate if the line is visible or not...
The best way would be creating a DataGrid and connecting two cells because it is still easier than two different listboxes.
This Code worked for me (here I get the coordinates of the selected item relative to its hosted window):
object selectedEntry = (object)myListBox.SelectedItem;
ListBoxItem lbi = this.myListBox.ItemContainerGenerator.ContainerFromItem(selectedEntry) as ListBoxItem;
Point p = lbi.TranslatePoint(new Point(0, 0), Window.GetWindow(lbi));

How to get controls from Grid.Row at runtime?

I have a Grid with 3 Rectangles in it. I need to get a reference to the rectangle that has Grid.Row == 3 at runtime.
How can I get access to it?
Thanks
var target = myGrid.Children
.Cast<UIElement>() // make it into IEnumerable<UIElement>
.OfType<Rectangle>() // and select only Rectangles
.Where(c => Grid.GetRow(c) == 3);
This will enumerate the children of your grid, and select only those which are of type Rectangle and have Grid.Row == 3. You can then use target.Single() or target.First() or any other query evaluation function to get access to the Rectangle.
Update:
Updated to address Ian's comment below. Very well said, I fully agree (didn't give too much thought to the original example code).

Animate ListBoxItem from one ListBox to another ListBox

I would like to create a visual effect when an item in a listbox is double clicked. So far I have drag and drop functionality where the item is visually attached to the mouse and can be moved to a drop target. From that functionality I am able to animate the item using the same logic of getting the item container, however I am not able to leave the items control. Is there any way to remove the item from the ListBox and visually animate it to another location? Basically the main list box is a hand of cards. When a card is double-clicked I want it to visually move from the hand listbox to a discard listbox. As of now, the logic for moving the item from one collection to another is no problem, however I would really like an animated visual representation of this event. Any ideas or references on how to do something like this would be appreciated.
Thanks,
Brandon
Further Details on what I have attempted:
There are some concepts that I do not yet have a strong grasp of yet, which has led me to run face first into this wall. I have a method that I pass in(some may be unnecessary) the ListBox as an ItemsControl, a FrameworkElement that is the listbox item, and the data object associated with the ListBox item. What I attempted to do was FindVisualChild of the ListBoxItem that is a canvas. I can do that. In my mind I was wanting to somehow clone the canvas either as a canvas or as a bitmap, add it to the children of the child of the page in the same location, remove the ListBoxItem from the ListBox, and animate the clone to the discard pile. When the animation completes the clone will be removed or hidden, and as that object is added to the discard pile collection it will effectively take the replace the clone.
My problem with this, is that I feel like there really is a simpler way of doing this using the adorner layer or something. I also, don't know how I would position the clone at the exact same position in the element further up the visual tree. I am going to keep working on it and researching other approaches, and I will just hope that someone will share some insight on this.
Here's some code I worked up to draw a visual to a bitmap. You may be able to adapt this to your needs, and draw the bitmap by adorning the UIElement that represents a common ancestor of the two list views. Note the use of FrameworkElement.TransformToAncestor to get the coordinates of a nested element in terms of an ancestor element.
public static BitmapSource CreateBitmapFromElement(FrameworkElement element, Double dpiX, Double dpiY)
{
Size elementSize = new Size(element.ActualWidth, element.ActualHeight);
Visual root = GetAdornerDecoratorAncestor(element);
Rect elementBounds = element.TransformToAncestor(root).TransformBounds(new Rect(elementSize));
RenderTargetBitmap rtb = new RenderTargetBitmap((Int32)(elementBounds.Size.Width * dpiX / 96.0),
(Int32)(elementBounds.Size.Height * dpiY / 96.0),
dpiX,
dpiY,
PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(root);
vb.ViewboxUnits = BrushMappingMode.Absolute;
vb.Stretch = Stretch.None;
vb.Viewbox = elementBounds;
dc.DrawRectangle(vb, null, new Rect(new Point(), elementBounds.Size));
}
rtb.Render(dv);
return rtb;
}
public static Visual GetAdornerDecoratorAncestor(DependencyObject obj)
{
while(obj != null && !(obj is AdornerDecorator))
{
obj = VisualTreeHelper.GetParent(obj);
}
return obj as AdornerDecorator;
}
OK, you could try taking a Visual element and setting it's background to a visualbrush of your ListItem and animate that to the other list box. You can wait for the storyboard completed event to actually do the switch. If this were me, I would animate from one box only to the edge of the other. If the switch happens fast enough it should look pretty seamless to the user. Finding the exact position of where the item is supposed to go into the list box would be pretty complex based on any sorting/filtering rules you have.
If the two listboxes are always in the same position, you could try animating the double-clicked item to a predetermined spot, let's say half-way between the old list and the new list. Then execute the code to move the item to the new list, but use a style that immediately starts an animation on that item starting it from that predetermined location and animating to its location in the new list. You'd probably have to tweak the initial offset of the new item at runtime based on where it's inserted in the list.

Resources