I'm trying to get the datagrid's scrollviewer to be able to set the offset (which has been stored earlier).
I use this function :
public static T GetVisualChild<T>(DependencyObject 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;
}
And I call it like this :
this.dataGrid.ItemsSource = _myData;
ScrollViewer sc = ressource_design.GetVisualChild<ScrollViewer>(this.dataGrid);
if (sc != null) sc.ScrollToVerticalOffset(stateDatagrid.ScrollbarOffset);
And it works in many cases, but in some cases the function returns null and I'm not able to get the scrollviewer.
This call is made just after setting the ItemsSource (ObservableCollection of items) and it works well in 90% cases. The datagrid has not been rendered yet.
I've also tried with the function :
public static ScrollViewer GetScrollViewerFromDataGrid(DataGrid dataGrid)
{
ScrollViewer retour = null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dataGrid) && retour == null; i++)
{
if (VisualTreeHelper.GetChild(dataGrid, i) is ScrollViewer)
{
retour = (ScrollViewer)(VisualTreeHelper.GetChild(dataGrid, i));
}
}
return retour;
}
still null.
Why I'm unable to get the datagrid's scrollviewer ?
I've not pasted my datagrid's style since I have datagrids working with it and it is complicated with many dependencies.
I thought it could be related to virtualization but i'm not able to retrieve the scrollviewer of this datagrid :
<DataGrid Style="{StaticResource StyleDataGrid}" HeadersVisibility="None" ItemsSource="{Binding _Data}" Name="dataGrid1" RowDetailsVisibilityMode="Visible" SelectionChanged="dataGrid1_SelectionChanged">
You need to go recursive through the VisualTree elements. Your function only looks at DataGrid layer. If the ScrollViewer isn't there (see picture) you will not find it.
Try the following function:
public static ScrollViewer GetScrollViewer(UIElement element)
{
if (element == null) return null;
ScrollViewer retour = null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element) && retour == null; i++) {
if (VisualTreeHelper.GetChild(element, i) is ScrollViewer) {
retour = (ScrollViewer) (VisualTreeHelper.GetChild(element, i));
}
else {
retour = GetScrollViewer(VisualTreeHelper.GetChild(element, i) as UIElement);
}
}
return retour;
}
Related
I am creating a WP 8.1 Silverlight app.
I have an ObservableCollection of string names, that is set to the ItemsSource of a ListBox. Which are the names of buttons in the ListBox. I then want to extract the buttons from the ListBox, but the return value is of type string.
The xaml code is:
<ListBox x:Name="Game_ScrollViewer_online" Margin="41,104,128,6" SelectedValuePath="Current_game_button">
<ListBox.ItemTemplate>
<DataTemplate>
<Button x:Name="Current_game_button" Content="{Binding}"
HorizontalAlignment="Center" Height="80" Margin="-14,6,-15,0"
VerticalAlignment="Top" Width="210" Template="{StaticResource Button_CurrentLayout1}"
RenderTransformOrigin="0.5,0.5" Foreground="#FFCBECCB" FontFamily="Times New Roman"
Click="LoadGame_online" FontSize="16">
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I then want to extract the button element:
for (int i = 0; i < Game_ScrollViewer_online.Items.Count; i++)
{
var tempType = Game_ScrollViewer_online.Items[i].GetType();
Button tempBut = (Game_ScrollViewer_online.Items[i] as Button);
//Do stuff with button
}
But as said the return type is string.
Why is it not button ? And is there a way to access the button?
You need FrameworkTemplate.FindName Method (String, FrameworkElement) for this purpose:
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;
}
Then:
for (int i = 0; i < Game_ScrollViewer_online.Items.Count; i++)
{
ListBoxItem GameListBoxItem = (ListBoxItem)(Game_ScrollViewer_online.ItemContainerGenerator.ContainerFromIndex(i));
ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(GameListBoxItem);
DataTemplate myDataTemplate = contentPresenter.ContentTemplate;
Button tempBut = (Button) myDataTemplate.FindName("Current_game_button", contentPresenter);
//Do stuff with button
}
To solve missing FindName use FindDescendant like this:
public T FindDescendant<T>(DependencyObject obj) where T : DependencyObject
{
if (obj is T)
return obj as T;
int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
if (childrenCount < 1)
return null;
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child is T)
return child as T;
}
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = FindDescendant<T>(VisualTreeHelper.GetChild(obj, i));
if (child != null && child is T)
return child as T;
}
return null;
}
I have a wpf application and I want to set everything to Focusable="false".
Is there an easy and elegant way? Currently I made a style for each type of Control I use like this:
<Style TargetType="Button">
<Setter Property="Focusable" Value="False"></Setter>
</Style>
Any idea for a more universan solution?
Why not try a two line solution ?
foreach (var ctrl in myWindow.GetChildren())
{
//Add codes here :)
}
Also make sure to add this :
public static IEnumerable<Visual> GetChildren(this Visual parent, bool recurse = true)
{
if (parent != null)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
// Retrieve child visual at specified index value.
var child = VisualTreeHelper.GetChild(parent, i) as Visual;
if (child != null)
{
yield return child;
if (recurse)
{
foreach (var grandChild in child.GetChildren(true))
{
yield return grandChild;
}
}
}
}
}
}
Or even shorter, use this :
public static IList<Control> GetControls(this DependencyObject parent)
{
var result = new List<Control>();
for (int x = 0; x < VisualTreeHelper.GetChildrenCount(parent); x++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, x);
var instance = child as Control;
if (null != instance)
result.Add(instance);
result.AddRange(child.GetControls());
}
return result;
}
I'm writing custom control where I switch default ItemsPanel with
ItemsPanelProperty.OverrideMetadata(typeof(DataGrid),
new FrameworkPropertyMetadata(new ItemsPanelTemplate(
new FrameworkElementFactory(typeof(AdvancedVirtualizingStackPanel)))));
How can I get reference to the instance of created AdvancedVirtualizingStackPanel?
Ok, I will answer myself.
First you need to get ItemsPresenter. You can use this code:
ItemsPresenter itemsPresenter =
(ItemsPresenter)GetTemplateChild(ItemsPresenterTemplateName);
// You have to name ItemsPresenter (using value of
// ItemsPresenterTemplateName string) in your XAML
Then:
itemsPresenter.ApplyTemplate(); // In case template is not applied yet
_stackPanel =
Helper.GetVisualChild<AdvancedVirtualizingStackPanel>(itemsPresenter);
And you got your panel.
Code for Helper.GetVisualChild():
public static T GetVisualChild<T>(DependencyObject 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;
}
I add pushpins dynamically to a Bing map. I also want to remove certain ones (based on a value embedded in their Tag property). In order to do this, do I need to identify the pushpin's MapOverlay, and if so, how might I go about that?
I am not sure which environment you are talking about, but it looks like its not windows 8.
So here is some code for windows phone 7.1. This assumes that you only have Pushpins in your map's children collection. If you also have other UI elements you would have to filter those out before going for the Tag property ;)
Pushpin t1 = new Pushpin();
t1.Tag = "t1";
map1.Children.Add(t1);
Pushpin t2 = new Pushpin();
t2.Tag = "t2";
map1.Children.Add(t2);
Pushpin t3 = new Pushpin();
t3.Tag = "t3";
map1.Children.Add(t3);
// LINQ query syntax
var ps = from p in map1.Children
where ((string)((Pushpin)p).Tag) == "t1"
select p;
var psa= ps.ToArray();
for (int i = 0; i < psa.Count();i++ )
{
map1.Children.Remove(psa[i]);
}
// or using method syntax
var psa2= map1.Children.Where(y => ((string)((Pushpin)y).Tag) == "t2").ToArray();
for (int i = 0; i < psa2.Count(); i++)
{
map1.Children.Remove(psa2[i]);
}
The map1 is defined in the apps main page; XAML like this:
<my:Map HorizontalAlignment="Stretch" Name="map1" VerticalAlignment="Stretch" />
I think this might work:
var pushPins = SOs_Classes.SOs_Utils.FindVisualChildren<Pushpin>(bingMap);
foreach (var pushPin in pushPins)
{
if (pushPin.Tag is SOs_Locations)
{
SOs_Locations locs = (SOs_Locations) pushPin.Tag;
if (locs.GroupName == groupToAddOrRemove)
{
bingMap.Children.Remove(pushPin);
}
}
}
// I got this from somebody somewhere, but forgot to note who/where
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null)
{
yield break;
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (var childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
How can I access the ItemsPanel of a ListBox at runtime in silverlight?
Given the following element declaration in XAML
<ListBox x:Name="LB" Loaded="LB_Loaded" />
There are two ways to achieve this, the easiest requires the Silverlight toolkit:
using System.Windows.Controls.Primitives;
private void LB_Loaded()
{
var itemsPanel = LB.GetVisualChildren().OfType<Panel>().FirstOrDefault();
}
Or you can use the VisualTreeHelper and write the following recursive method:
T GetFirstChildOfType<T>(DependencyObject visual) where T:DependencyObject
{
var itemCount = VisualTreeHelper.GetChildrenCount(visual);
if (itemCount < 1)
{
return null;
}
for (int i = 0; i < itemCount; i++)
{
var dp = VisualTreeHelper.GetChild(visual, i);
if (dp is T)
{
return (T)dp;
}
}
for (int i = 0; i < itemCount; i++)
{
var dp = GetFirstChildOfType<T>(VisualTreeHelper.GetChild(visual, i));
if (dp != null) return dp;
}
return null;
}
And get the result in a similar manner:
void ItemsPanelSample_Loaded(object sender, RoutedEventArgs e)
{
var itemsPanel = GetFirstChildOfType<Panel>(LB);
}
Building on terphi's solution, i changed it to return a list of the elements you are looking for as normally when your searching for a type, the listbox will have multiple items and multiple instances of what you are looking for. Additionally, I had issues with it finding things in the loaded event but used a dispatcher instead and it finds the items every time in testing.
private List<TextBlock> TextBlockList;
in the constructor, after associating the datasource with listbox:
Dispatcher.BeginInvoke(delegate { TextBlockList = GetFirstChildOfType<TextBlock>(listBox1); });
List<T> GetFirstChildOfType<T>(DependencyObject visual) where T : DependencyObject
{
DependencyObject ControlCandidate;
List<T> TempElements;
List<T> TargetElementList = new List<T>();
var itemCount = VisualTreeHelper.GetChildrenCount(visual);
if (itemCount > 0)
{
for (int i = 0; i < itemCount; i++)
{
ControlCandidate = VisualTreeHelper.GetChild(visual, i);
if (ControlCandidate is T)
TargetElementList.Add((T)ControlCandidate);
}
for (int i = 0; i < itemCount; i++)
{
TempElements = GetFirstChildOfType<T>(VisualTreeHelper.GetChild(visual, i));
if (TempElements.Count > 0)
TargetElementList.AddRange(TempElements);
}
}
return TargetElementList;
}