Visual Tree Helper makes listbox SelectionChanged fire twice - c#

Visual tree code
public 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;
}
}
}
}
selectionChanged code
private void mylistBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (mylistBox.SelectedItem == null)
return;
foreach (Button tb in FindVisualChildren<Button>(mainListBox))
{
Debug.WriteLine(tb.Name);
}
}
output
Note it writes the name of the two button twice so it loops 4 times and it only has 2 buttons.
optionBtn
optionBtn2
optionBtn
optionBtn2

How many items are there in mainListBox at runtime? It must be 2 and that's why it print the buttons names twice (2 times for each item in the list box). There is nothing wrong with SelectionChanged or VisualTreeHelper

Related

Check more than one checkbox upon clicking a button c#

I have for example:
Checkbox1.IsChecked = true;
Checkbox2.IsChecked = true;
Checkbox3.IsChecked = true;
I have this 32 times. Is there a way to have concat string? For example:
i = 1
while i < 32:
("Checkbox"+ (i)).IsChecked = true;
i++
Thanks
While you cannot do exactly what you intend, you can check or uncheck all checkboxes that are in a given container. For example, let's say you have a Panel that contains a number of checkboxes, called pnlChecks. You could do something like
foreach (var chkBox in pnlChecks.Controls.OfType<CheckBox>())
{
chkBox.IsChecked = true;
}
There are multiple methods to achieve this.
Add all of them to a generic List<> and iterate through them like the for you mentioned.
Use reflection and get the checkbox controls and set their value.
Sample WinForms Code
private List<CheckBox> checkboxes = new List<CheckBox>();
public Form1()
{
InitializeComponent();
FillCheckboxes();
}
private void CheckAll()
{
foreach (var chk in checkboxes)
{
chk.Checked = true;
}
}
private void FillCheckboxes()
{
foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
checkboxes.Add(c as CheckBox);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
CheckAll();
}
Sample WPF Code
private List<CheckBox> checkboxes = new List<CheckBox>();
public Window1()
{
InitializeComponent();
checkboxes = FindVisualChildren<CheckBox>(main).ToList();
CheckAll();
}
private void CheckAll()
{
foreach (var chk in checkboxes)
{
chk.IsChecked = true;
}
}
public 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;
}
}
}
}
Important Note
For WPF the suggested method is to use data binding instead of iterating through controls and manually checking/unchecking them. Just bind the IsChecked property to the desired value and change it. You can find more info regarding this on numerous articles on the Internet.

How to programatically expand all expanders in window

I've got a window with some expanders in it.
When you open a expander there is some information inside it.
What i need to do is to open all expanders with one button so everything inside them becomes visible.
When everything is visible i want to print the full page.
This is my code for expanding all expanders now:
public 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;
}
}
}
}
The lines i use to iterate through my controls:
foreach (Expander exp in FindVisualChildren<Expander>(printpage))
{
exp.IsExpanded = true;
}
Now to the point:
The code above works in most cases.
The only problem i have is that sometimes there are some expanders WITHIN expanders.
The parent expanders do expand when the above code executes, The child expanders however remain unexpanded.
I hope someone can teach me how to expand those child expanders too.
EDIT
I forgot to mention that the child-expanders are not direct childs of the main expanders..
They are children of children of children of the main expanders.
My controll-tree goes something like this:
-Stackpanel
---List item
-----Grid
-------Expander (Main expanders)
---------Grid
-----------Textblock
-------------Expander
So i need to expand all expanders in this tree.
Your code is quite complicated already for what it does. Yields are absolutely not necessary if you call and you really should to execute your method in a recursive fashion.
When, inside your method you encounter a control with children, you call the same method but with a new visual root, which will be a control with children you've just found.
This should work for you (may be a few syntax errors but i'm sure if there are you can fix them)
foreach (Expander exp in FindVisualChildren<Expander>(printpage))
{
exp.IsExpanded = true;
for(int i =0;i<exp.Children.Count;i++)
{
if(exp.Children[i] is Expander)
{
expandChildren(exp.Children[i]);
}
}
}
private expandChildren(Expander exp)
{
exp.IsExpanded = true;
for(int i =0;i<exp.Children.Count;i++)
{
if(exp.Children[i] is Expander)
{
expandChildren(exp.Children[i]);
}
}
}
Okay, I found my anwser in this post
The anwser on this question is what I used to solve my problem.
Here's the function I used:
public static List<T> GetLogicalChildCollection<T>(object parent) where T : DependencyObject
{
List<T> logicalCollection = new List<T>();
GetLogicalChildCollection(parent as DependencyObject, logicalCollection);
return logicalCollection;
}
private static void GetLogicalChildCollection<T>(DependencyObject parent, List<T> logicalCollection) where T : DependencyObject
{
IEnumerable children = LogicalTreeHelper.GetChildren(parent);
foreach (object child in children)
{
if (child is DependencyObject)
{
DependencyObject depChild = child as DependencyObject;
if (child is T)
{
logicalCollection.Add(child as T);
}
GetLogicalChildCollection(depChild, logicalCollection);
}
}
}
In my code I used these lines to append what I needed to my expanders:
List<Expander> exp = GetLogicalChildCollection<Expander>(printpage.StackPanelPrinting);
foreach (Expander exp in expander)
{
exp.IsExpanded = true;
exp.FontWeight = FontWeights.Bold;
exp.Background = Brushes.LightBlue;
}

Finding ALL child controls WPF

I would like to find all of the controls within a WPF control. I have had a look at a lot of samples and it seems that they all either require a Name to be passed as parameter or simply do not work.
I have existing code but it isn't working properly:
public 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;
}
}
}
}
For instance it will not get a DataGrid within a TabItem.
Any suggestions?
You can use these.
public static List<T> GetLogicalChildCollection<T>(this DependencyObject parent) where T : DependencyObject
{
List<T> logicalCollection = new List<T>();
GetLogicalChildCollection(parent, logicalCollection);
return logicalCollection;
}
private static void GetLogicalChildCollection<T>(DependencyObject parent, List<T> logicalCollection) where T : DependencyObject
{
IEnumerable children = LogicalTreeHelper.GetChildren(parent);
foreach (object child in children)
{
if (child is DependencyObject)
{
DependencyObject depChild = child as DependencyObject;
if (child is T)
{
logicalCollection.Add(child as T);
}
GetLogicalChildCollection(depChild, logicalCollection);
}
}
}
You can get child button controls in RootGrid f.e like that:
List<Button> button = RootGrid.GetLogicalChildCollection<Button>();
You can use this Example:
public Void HideAllControl()
{
/// casting the content into panel
Panel mainContainer = (Panel)this.Content;
/// GetAll UIElement
UIElementCollection element = mainContainer.Children;
/// casting the UIElementCollection into List
List < FrameworkElement> lstElement = element.Cast<FrameworkElement().ToList();
/// Geting all Control from list
var lstControl = lstElement.OfType<Control>();
foreach (Control contol in lstControl)
{
///Hide all Controls
contol.Visibility = System.Windows.Visibility.Hidden;
}
}

WPF: Get next /prev visual object

I have a StackPanel containing a number ofTextBox. Is there a way to get the Next / Previous visual elements ?
The functionality I want is fairly analogous to jQuery's .next() function that get the next object.
You can try following method to enumerate the Visual Tree.
public static IEnumerable<T> FindVisualChildren<T>
(DependencyObject depObj, string childName) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
var frameworkElement = child as FrameworkElement;
if (child != null && frameworkElement.Name == childName)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child, childName))
{
yield return childOfChild;
}
}
}
}
Assuming that you named your textBoxes "tbInsideStackPanel", use it like:
foreach (var textBox in FindVisualChildren<TextBox>(this.stackPanel1,
"tbInsideStackPanel").ToList())
{
textBox.Background = Brushes.Blue;
}

WPF Listbox auto scroll while dragging

I have a WPF app that has a ListBox. The drag mechanism is already implemented, but when the list is too long and I want to move an item to a position not visible, I can't.
For example, the screen shows 10 items. And I have 20 items. If I want to drag the last item to the first position I must drag to the top and drop. Scroll up and drag again.
How can I make the ListBox auto scroll?
Got it. Used the event DragOver of the ListBox, used the function found here to get the scrollviewer of the listbox and after that its just a bit of juggling with the Position.
private void ItemsList_DragOver(object sender, System.Windows.DragEventArgs e)
{
ListBox li = sender as ListBox;
ScrollViewer sv = FindVisualChild<ScrollViewer>(ItemsList);
double tolerance = 10;
double verticalPos = e.GetPosition(li).Y;
double offset = 3;
if (verticalPos < tolerance) // Top of visible list?
{
sv.ScrollToVerticalOffset(sv.VerticalOffset - offset); //Scroll up.
}
else if (verticalPos > li.ActualHeight - tolerance) //Bottom of visible list?
{
sv.ScrollToVerticalOffset(sv.VerticalOffset + offset); //Scroll down.
}
}
public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
// Search immediate children first (breadth-first)
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;
}
Based on this I have created an Attached Behavior which can easily be used like this -
<ListView
xmlns:WpfExtensions="clr-namespace:WpfExtensions"
WpfExtensions:DragDropExtension.ScrollOnDragDrop="True"
Here is the code for attached behavior -
/// <summary>
/// Provides extended support for drag drop operation
/// </summary>
public static class DragDropExtension
{
public static readonly DependencyProperty ScrollOnDragDropProperty =
DependencyProperty.RegisterAttached("ScrollOnDragDrop",
typeof(bool),
typeof(DragDropExtension),
new PropertyMetadata(false, HandleScrollOnDragDropChanged));
public static bool GetScrollOnDragDrop(DependencyObject element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
return (bool)element.GetValue(ScrollOnDragDropProperty);
}
public static void SetScrollOnDragDrop(DependencyObject element, bool value)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
element.SetValue(ScrollOnDragDropProperty, value);
}
private static void HandleScrollOnDragDropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement container = d as FrameworkElement;
if (d == null)
{
Debug.Fail("Invalid type!");
return;
}
Unsubscribe(container);
if (true.Equals(e.NewValue))
{
Subscribe(container);
}
}
private static void Subscribe(FrameworkElement container)
{
container.PreviewDragOver += OnContainerPreviewDragOver;
}
private static void OnContainerPreviewDragOver(object sender, DragEventArgs e)
{
FrameworkElement container = sender as FrameworkElement;
if (container == null)
{
return;
}
ScrollViewer scrollViewer = GetFirstVisualChild<ScrollViewer>(container);
if (scrollViewer == null)
{
return;
}
double tolerance = 60;
double verticalPos = e.GetPosition(container).Y;
double offset = 20;
if (verticalPos < tolerance) // Top of visible list?
{
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - offset); //Scroll up.
}
else if (verticalPos > container.ActualHeight - tolerance) //Bottom of visible list?
{
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offset); //Scroll down.
}
}
private static void Unsubscribe(FrameworkElement container)
{
container.PreviewDragOver -= OnContainerPreviewDragOver;
}
private static T GetFirstVisualChild<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)
{
return (T)child;
}
T childItem = GetFirstVisualChild<T>(child);
if (childItem != null)
{
return childItem;
}
}
}
return null;
}
}

Categories

Resources