I am working on a UWP application trying to implement the Transform3DAnimations as from the platform samples using a GridView instead of a HubSection. I've got it all figured out except for fetching the GridViewItem. The below code is from the sample in context to a hubsection.
private void UpdateRandomSection()
{
// Updating the section triggers a cool animation!
// See SectionView.xaml and SectionView.xaml.cs
var sectionsInView = HeadlinesHub.SectionsInView;
var sectionsCount = sectionsInView.Count;
if (sectionsCount > 0)
{
var sectionToUpdate = sectionsInView[_random.Next(sectionsCount)];
sectionToUpdate.DataContext = new HeroArticlesViewModel();
}
}
I am trying to fetch the GridViewItem, but I am unable to fetch the GridViewItem it always returns me the data model of the GridViewItem. How can I get the GridViewItem from the GridView? My code is below:
private Random InAppLiveTileRandomTileNumberGenerator;
private void UpdateRandomSection()
{
var sectionsInView = AllDevicesGridView.Items;
var sectionsCount = sectionsInView.Count;
if (sectionsCount > 0)
{
var sectionToUpdate = (GridViewItem)AllDevicesGridView.Items[InAppLiveTileRandomTileNumberGenerator.Next(sectionsCount)]; //Invalid Cast exception here
sectionToUpdate.DataContext = new InappLiveTileViewModelModel();
}
}
Link to the sample
Solution I tried from stack answers
For your code line var sectionToUpdate = (GridViewItem)AllDevicesGridView.Items[InAppLiveTileRandomTileNumberGenerator.Next(sectionsCount)]; it will returns the data model of the GridViewItem as you seen. To get the GridViewItem you may need to use ContainerFromItem method. For example, if you want to get the GridViewItem object from the selected item code may as follows:
private void listViewDirectory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var container = AllDevicesGridView.ContainerFromItem(AllDevicesGridView.SelectedItem);
GridViewItem item = container as GridViewItem;
System.Diagnostics.Debug.WriteLine(item.ActualHeight);
}
It seems like you want get a special item with item index assigned:
var container = AllDevicesGridView.ContainerFromItem(AllDevicesGridView.Items[1]);
GridViewItem item = container as GridViewItem;
If you want to get data context of the GridViewItem, you may need to get the ListViewItemPresenter from GridViewItem. Here I use the VisualTreeHelper to get the ListViewItemPresenter. By the way, VisualTreeHelper is not recommended to use unless you don't have other ways.
private void listViewDirectory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var container = AllDevicesGridView.ContainerFromItem(AllDevicesGridView.Items[1]);
GridViewItem item = container as GridViewItem;
ListViewItemPresenter itempresenter;
IEnumerable<ListViewItemPresenter> items = FindVisualChildren<ListViewItemPresenter>(item);
int count = items.Count();
itempresenter = items.ElementAt<ListViewItemPresenter>(0);
itempresenter.DataContext = new Person() { Name = "update", Contact = "update" };
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
Related
Good day
I must be missing something really simple here; I have a datagrid with two combobox columns and a datagridtextcolumn. Once the user has selected a value for both of these comboboxes (a value ranging from 1 to 5), a calculation is done with these 2 values (on a combobox selection changed event). I need to write the result of the calculation in to a cell (the datagridtextcolumn) and change the background of that cell to a specific colour based on the value. I have managed to find the values in the comboboxes and the index of the current row, but accessing the relevant cell to write the result to is getting the better of me...
Here is the coding for the event handler that calls the calculation thus far:
private void cmbConseq_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var cellInfos = dgDeviation.SelectedCells;
if (cellInfos == null)
{
return;
}
var content = cellInfos[0].Column.GetCellContent(cellInfos[0].Item);
DataRowView row = (DataRowView)content.DataContext;//get the datacontext from FrameworkElement and typecast to DataRowView
object[] obj = row.Row.ItemArray;//ItemArray returns an object array with single element
int likely = Convert.ToInt16(obj[0]);
int cons = Convert.ToInt16(obj[1]);
int riskValue = new Calculations().riskRankingCalc(likely, cons);
int rowIndex = dgDeviation.Items.IndexOf(dgDeviation.CurrentItem);
How do I write the 'riskValue' variable to the relevant cell in the currently selected row in the eventhandler?
After a bit of experimentation and help from other SO questions, the solution that worked for me was to implement the following two helper methods:
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
public static DataGridCell GetCell(DataGrid grid, int column)
{
DataGridRow row = grid.ItemContainerGenerator.ContainerFromIndex(grid.SelectedIndex) as DataGridRow;
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
if (presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}
And then to make the change in the cell is as easy as calling: GetCell(myDatagrid, columnIndex).Content = "New Value";
I a beginner in C# and WPF. I'm programming plugin for a node based software called vvvv. I have implemented sliders, buttons and other simple ui elements. The following code shows how a sliders node look in c# :
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Xml;
using VVVV.PluginInterfaces.V2;
namespace VVVV.Packs.UI.Nodes.WPF
{
[PluginInfo(Author = "lecloneur", Category = "WPF", Help = "WPF Slider", Name = "Slider", AutoEvaluate = false)]
public class WPFSlider : GenericNode, IPluginEvaluate
{
[Input("SizeX", DefaultValue = 120, Order = 9, MinValue = 0)]
public IDiffSpread<int> SizeX;
[Input("SizeY", DefaultValue = 120, Order = 9, MinValue = 0)]
public IDiffSpread<int> SizeY;
[Input("Orientation", Order = 1, DefaultEnumEntry = "Horizontal")]
public IDiffSpread<Orientation> OrientationIn;
[Output("Value", Order = 2)]
public ISpread<double> ValueOut;
int elements_count = 0;
public void Evaluate(int SpreadMax)
{
UIElementOut.SliceCount = SpreadMax;
ValueOut.SliceCount = SpreadMax;
for (int i = 0; i < SpreadMax; i++)
{
if (UIElementOut == null || !(UIElementOut[0] is Slider) || elements_count < SpreadMax || OrientationIn.IsChanged || SizeX.IsChanged || SizeY.IsChanged)
{
CreateElement(i);
}
OutputData(i);
Transformation(i, (Slider)UIElementOut[i]);
}
elements_count = SpreadMax;
}
private void CreateElement(int i)
{
UIElementOut[i] = new Slider();
var uiElement = (Slider)UIElementOut[i];
uiElement.Minimum = 0;
uiElement.Maximum = 1;
uiElement.Orientation = OrientationIn[i];
uiElement.IsMoveToPointEnabled = true;
uiElement.Width = SizeX[i]; ;
uiElement.Height = SizeY[i];
uiElement.VerticalAlignment = VerticalAlignment.Center;
uiElement.HorizontalAlignment = HorizontalAlignment.Center;
XmlReader XmlRead = XmlReader.Create("Styles/SliderStyle.xaml");
ResourceDictionary myResourceDictionary = (ResourceDictionary)XamlReader.Load(XmlRead);
XmlRead.Close();
Style uiElementStyle = myResourceDictionary["SliderStyle"] as Style;
uiElement.Style = uiElementStyle;
}
private void OutputData(int i)
{
var uiElement = (Slider)UIElementOut[i];
ValueOut[i] = uiElement.Value;
}
}
}
Now I'm trying to implement a tabcontrol where I could dynamically create tabitem and input UIElement into it. As far as I understand, I can only add one things to a tabitem. So I was thinking about creating a grid everytime I need to and fill it with all the incoming UIElement.
public void Evaluate(int SpreadMax)
{
SpreadMax = 1;
UIElementOut.SliceCount = 1;
for (var i = 0; i < SpreadMax; i++)
{
if (UIElementOut == null || !(UIElementOut[i] is TabControl))
UIElementOut[i] = new TabControl();
var uiElement = (TabControl)UIElementOut[i];
uiElement.Height = 200;
uiElement.Width = 500;
}
Grid grid;
int[] _elementCounts = new int[_elementInputs.SliceCount];
for (var i = 0; i < _elementInputs.SliceCount; i++)
{
if (_elementInputs[i] == null || !(_elementInputs[i] is UIElement))
{
grid = new Grid();
for (var j = 0; j < _elementInputs[i].IOObject.SliceCount; j++)
{
if (_elementInputs[i].IOObject[j] != null)
{
UIElement test = new UIElement();
test = _elementInputs[i].IOObject[j];
grid.Children.Add(test);
}
}
_elementCounts[i] = _elementInputs[i].IOObject.SliceCount;
ValueOut[i] = _elementCounts[i];
if (((TabControl)UIElementOut[0]).Items.Count <= i)
{
((TabControl)UIElementOut[0]).Items.Add(new TabItem { Header = _nameInputs[i].IOObject[0], Content = grid });
}
if (_nameInputs[i].IOObject.IsChanged)
{
((TabItem)((TabControl)UIElementOut[0]).Items[i]).Header = _nameInputs[i].IOObject[0];
}
if (_elementInputs[i].IOObject.IsChanged)
{
((TabItem)((TabControl)UIElementOut[0]).Items[i]).Content = grid;
}
}
}
for (var i = ((TabControl)UIElementOut[0]).Items.Count - 1; ((TabControl)UIElementOut[0]).Items.Count > _elementInputs.SliceCount; i--)
{
((TabControl)UIElementOut[0]).Items.RemoveAt(i);
}
}
I searched a lot but can't find any idea how to solve the error. Apparently adding elements to a the grid throw "specified element is already the logical child of another element";
Hi please try the next method on a several visual objects (child) and check if the resulted object is the same reference. Here is a usefull link with explanations and more...
Extension class code:
public static class VisualTreeHelperExtensions
{
public static T FindParent<T>(this DependencyObject child) where T : DependencyObject
{
while (true)
{
//get parent item
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
T parent = parentObject as T;
if (parent != null)
return parent;
child = parentObject;
}
}
}
Example:
var dataGrid1 = dependencyObject1.FindParent<DataGrid>();
var dataGrid2 = dependencyObject2.FindParent<DataGrid>();
var isSameObject = dataGrid1 == dataGrid2;
Updates
The grid can contains a large number of elements but the only last will be visible to user.
The error is coming from the elemnt you want to add itself, that elemnt is belong to another control (some another control has the element as a child).
Find the parent element of the element you want to add, remove the element from the current parent's children collection, and add this element as the new child of your grid.
Try to use snoop to figure out who is the parent of your element (containing in _elementInputs).
Here are some useful links (first, second).
Update 2
As I can understand you have a third party infrastructure in your project because I can't resolve the type of _elementInputs and UIElementOut collections.
Since the _elementInputs is a field, I still can't understand where is _elementInputs came from (can't see that in code).
The code to add a completely new element is wrong:
Correct code(in my opinion)
//here is in my opinion the correct algorithm in this case
var elementFromInputs = _elementInputs[i] as UIElement;
if (elementFromInputs == null) continue;
//try to find parent of type Panel
var parentPanel = elementFromInputs.FindParent<Panel>();
//try to find parent of type ContentControl
var parentContentControl = elementFromInputs.FindParent<ContentControl>();
if (parentPanel == null && parentContentControl == null)
{
//add if there is no any parents
grid.Children.Add(elementFromInputs);
}
if (parentPanel != null)
{
//remove element from parent's collection
parentPanel.Children.Remove(elementFromInputs);
}
if(parentContentControl != null)
{
//reset parent content to release element
parentContentControl.Content = null;
}
grid.Children.Add(elementFromInputs);
Update 3
You've pasted the code (from the correct code section) inside the if which condition is the _elementInputs[i] == null || !(_elementInputs[i] is UIElement) that means filtering all your UIElements out of the scope. Since I'm not familiar with the vvvv concepts I don't know what do you have inside the _elementInputs array, but if you have the UIElements there you need past the code I gave you before the if with condition _elementInputs[i] == null || !(_elementInputs[i] is UIElement).
Please update your question with the next clarifications:
1. What is inside the _elementInputs[i]?
2. What is inside the _elementInputs[i].IOObject?
3. What are UIElements you want to add to the grid?
4. Please run the next method and write me the comment what do you have in your grid and TabControl controls.
Test code
public void Evaluate(int SpreadMax)
{
SpreadMax = 1;
UIElementOut.SliceCount = 1;
for (var i = 0; i < SpreadMax; i++)
{
if (UIElementOut == null || !(UIElementOut[i] is TabControl))
UIElementOut[i] = new TabControl();
var uiElement = (TabControl)UIElementOut[i];
uiElement.Height = 200;
uiElement.Width = 500;
}
Grid grid = new Grid();
var listOfElements = new List<UIElements>
{
new Button {Background = Brushes.Tomato, Content = "Click Me"},
new Button {Background = Brushes.Yellow, Content = "Click Me"},
new Button {Background = Brushes.Green, Content = "Click Me"},
new Button {Background = Brushes.Blue, Content = "Click Me"}
};
listOfElements.ForEach(button => grid.Children.Add(button));
((TabControl)UIElementOut[0]).Items.Add(new TabItem { Header = "Objects", Content = grid });
}
I'll be glad to help if you will have problems with the code.
Regards.
I have a DataGrid where the ItemsSource is bound to an ObservableCollection<LogEntry>. On a click on a Button the user can scroll to a specific LogEntry. Therefor I use the following code:
private void BringSelectedItemIntoView(LogEntry logEntry)
{
if (logEntry != null)
{
ContentDataGrid.ScrollIntoView(logEntry);
}
}
This just works fine. But what I don't like is: If the LogEntry already is in view then the DataGrid flickers shortly.
My question now is:
Is there a possibility to check on the DataGrid if the given LogEntry already is in view?
You can get index for first visible item and last visible item
then you can check if your item's index was within first and last or not.
var verticalScrollBar = GetScrollbar(DataGrid1, Orientation.Vertical);
var count = DataGrid1.Items.Count;
var firstRow = verticalScrollBar.Value;
var lastRow = firstRow + count - verticalScrollBar.Maximum;
// check if item index is between first and last should work
Get Scrollbar method
private static ScrollBar GetScrollbar(DependencyObject dep, Orientation orientation)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dep); i++)
{
var child = VisualTreeHelper.GetChild(dep, i);
var bar = child as ScrollBar;
if (bar != null && bar.Orientation == orientation)
return bar;
else
{
ScrollBar scrollBar = GetScrollbar(child, orientation);
if (scrollBar != null)
return scrollBar;
}
}
return null;
}
I have a listbox , I add items dynamically to the listbox.
I want the listbox to autoscroll to the last item added.
I used
List<string> ItemsList = new List<string>
public void InsertItem(string newItem)
{
ItemsList.Add(status);
if (ItemsList.Count > MaxSize)
{
ItemsList.RemoveAt(0);
}
lb.Items.Refresh();
lb.SelectedIndex = lb.Items.Count - 1;
lb.ScrollIntoView(status);
}
but this works only before my application is initialized (i.e I added some items before the application starts)
But after the application has started if I try adding items,the scroll bar is not auto scrolling to the last added item
Could any one tell solution for this
This has turned out to be quite a mission actually, because the ScrollIntoView only works the first time it is called. Every other call after that will not work for some reason.
The way around this, would be to find the "ScrollInfo" of the listbox and set the scroll value. See example below
public static void AutoScrollToCurrentItem(ListBox listBox, int index)
{
// Find a container
UIElement container = null;
for (int i = index; i > 0; i--)
{
container = listBox.ItemContainerGenerator.ContainerFromIndex(i) as UIElement;
if (container != null)
{
break;
}
}
if (container == null)
return;
// Find the ScrollContentPresenter
ScrollContentPresenter presenter = null;
for (Visual vis = container; vis != null && vis != listBox; vis = VisualTreeHelper.GetParent(vis) as Visual)
if ((presenter = vis as ScrollContentPresenter) != null)
break;
if (presenter == null)
return;
// Find the IScrollInfo
var scrollInfo =
!presenter.CanContentScroll ? presenter :
presenter.Content as IScrollInfo ??
FirstVisualChild(presenter.Content as ItemsPresenter) as IScrollInfo ??
presenter;
// Find the amount of items that is "Visible" in the ListBox
var height = (container as ListBoxItem).ActualHeight;
var lbHeight = listBox.ActualHeight;
var showCount = (int)(lbHeight / height) - 1;
//Set the scrollbar
if (scrollInfo.CanVerticallyScroll)
scrollInfo.SetVerticalOffset(index - showCount);
}
private static DependencyObject FirstVisualChild(Visual visual)
{
if (visual == null) return null;
if (VisualTreeHelper.GetChildrenCount(visual) == 0) return null;
return VisualTreeHelper.GetChild(visual, 0);
}
The way to use the above code can be like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Add("New Item");
AutoScrollToCurrentItem(listBox1, listBox1.Items.Count);
}
I really wish I had enough points to comment on your question. How is your ItemsList connected to your ListBox? After you start your application, can you manually scroll down to the new items?
Most likely you binded ItemsList to ListBox. If so, I suggest you change List<string> to ObservableCollection<string> so that ListBox can automatically update itself.
ObservableCollection
if u can update listbox automatically then use ObservableCollection.
private ObservableCollection<string> _source = new ObservableCollection<string>();
and your insert method:
private void Insert(string item)
{
_source.Add(item);
lb.SelectedIndex = _source.Count - 1;
lb.ScrollIntoView(ListBox.SelectedItem);
}
My problem is about handling drag and drop in a ListView.
So I get the selected ListViewItem.
ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");
If i move a new element via drag&drop (for example from windows explorer), the itemCollection equals null, because i dont select an item in the listview.
private void DragDropHandler(object sender, DragEventArgs e)
{
ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");
if (itemCollection == null)
{
itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView");
}
}
for this case I would get the last element in list view, how can i do that?
Try this:
var r = Enumerable.Empty<ListViewItem>();
if(this.listView1.Items.Count > 0)
r = this.listView1.Items.OfType<ListViewItem>();
var last = r.LastOrDefault();
if (last != null)
{
// do your stuff
}