Recursive search cant find tabitem content (only header) [WPF | C#] - c#

I've been busy experimenting with UWP and WPF. After some getting used to, sometimes stupid, quirks, I decided to make one of my signature... "way overscoped" projects in WPF.
Anyway I'm making an application where I need to bind properties in a static class to UI elements (and when the properties change the UI elements need to change too). I know bindings exist but I've been trying for ages to get the UI to update when the property changes (with INotifyPropertyChanged and the PropertyChanged eventhandler). Eventually, i gave up and decided to make my own binding system(kinda anyway... I've got expansions planned, which is why i want it to be custom).
working of the code:
[Design Time]
Basically, what i have to do is make a property in the VNClient class, add a BindingAttribute(string bindingName) to it and set the Tag of the UI element i want to bind it to to the bindingName. I've got that setup.
[Runtime (only once at startup)]
Now the code will get all properties from the VNClient class with a BindingAttribute and add them to a dictionary as keys, then it will recursively look through the XAML hierarchy and any element with a tag that is also in the dictionary (meaning its bindable) will be added as a value to the dictionary.
[Runtime (every time a property changes)]
An event is fired telling the BindingManager which property changed. It will then get that property name from the dictionary (along with a dependency property but that's not implemented yet) to see which UI elements are bound to that property, then it will change the correct property to the correct value.
Here is the BindingManager:
internal class BindingManager
{
Dictionary<string, List<FrameworkElement>> staticReferenceBindings = new();
public BindingManager()
{
VNClient.PropertyChanged += VNClient_PropertyChanged;
MainWindow.ApplicationLoaded += MainWindow_ApplicationLoaded;
}
private void MainWindow_ApplicationLoaded(object? sender, EventArgs e)
{
foreach (PropertyInfo property in typeof(VNClient).GetProperties())
{
BindingAttribute attr;
if ((attr = (BindingAttribute)property.GetCustomAttribute(typeof(BindingAttribute), false)) != null)
{
staticReferenceBindings.Add(property.Name, null);
}
}
FindBindings(VNClient.MainWindowInstance);
}
private async void VNClient_PropertyChanged(object? sender, (string bindTag, DependencyProperty bindProperty, dynamic value) e)
{
foreach (KeyValuePair<string, List<FrameworkElement>> Binding in staticReferenceBindings)
{
if (Binding.Value == null) continue;
foreach (FrameworkElement element in Binding.Value)
{
DependencyProperty modifiedProperty = e.bindProperty;
//Property conversion for different elements... like background property => fill property
if (Binding is Shape && e.bindProperty == Control.BackgroundProperty) modifiedProperty = Shape.FillProperty;
else if (Binding is Window && e.bindProperty == TextBlock.TextProperty) modifiedProperty = Window.TitleProperty;
if (modifiedProperty != null) element.SetValue(modifiedProperty, e.value);
}
}
}
internal void FindBindings(DependencyObject parent)
{
int childCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childCount; i++)
{
DependencyObject dpObject = VisualTreeHelper.GetChild(parent, i);
FrameworkElement child = dpObject as FrameworkElement;
if (child != null)
{
string childTag = child.Tag?.ToString();
if (childTag != null && staticReferenceBindings.ContainsKey(childTag))
{
if (staticReferenceBindings[childTag] == null) staticReferenceBindings[childTag] = new List<FrameworkElement>();
staticReferenceBindings[childTag].Add(child);
}
}
FindBindings(dpObject);
}
}
}
Here is an example property:
internal static event EventHandler<(string bindTag, DependencyProperty bindProperty, dynamic value)> PropertyChanged;
private static string _gameName = "*Insert name here :)*";
[BindingAttribute(nameof(GameName))]
public static string GameName
{
get
{
return _gameName;
}
set
{
if (_gameName != value) _gameName = value;
OnPropertyChanged(nameof(GameName), TextBlock.TextProperty, value);
}
}
private static void OnPropertyChanged(string bindTag, DependencyProperty bindProperty, dynamic value) => PropertyChanged?.Invoke(Application.Current, (bindTag, bindProperty, value));
And here is that property bound to a TextBlock:
<TabItem Height="60" Width="250" BorderThickness="1" Background="Transparent" BorderBrush="Black" Foreground="White">
<TabItem.Header>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Image HorizontalAlignment="Left" Source="/Res/info_96px.png" Margin="0,0,180,0"/>
<TextBlock Text="About" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextTrimming="None" Foreground="#BFFFFFFF"/>
</Grid>
</TabItem.Header>
<StackPanel>
<Image Source="/Res/Logo.png" HorizontalAlignment="Center" VerticalAlignment="Top" Height="150" Width="150"/>
<TextBlock Text="Made with *Insert name here :)*" HorizontalAlignment="Center"/>
<WrapPanel HorizontalAlignment="Center">
<!-- EXAMPLE BINDING --><TextBlock Text="{x:Static local:VNClient.GameName}" Tag="GameName" Margin="0,30,5,0" HorizontalAlignment="Center"/>
<TextBlock Text="was made with *Insert name here :)* version:" Margin="0,30,0,0" HorizontalAlignment="Center"/>
<TextBlock Text="{x:Static local:VNClient.EngineVersion}" Tag="EngineVersion" Margin="5,30,0,0" HorizontalAlignment="Center"/>
</WrapPanel>
</StackPanel>
</TabItem>
(static binding is so i can see the binding in the VS editor)
Okay, so, everything works fine BUT when this XAML element is in a TabItem my recursive search can only find the
<TabItem.Header/>
content NOT the
<TabItem.Content/>
meaning the bindings won't update... which is kinda not good...
If anyone has any idea besides "Just use the normal bindings..." that would be amazing
Thanks in advance :)
(and sorry if this is hard to read i am dyslexic)
EDIT:
I got it working by explicitly specifying, if it's a TabItem start another recursive search through it's content before continuing with it's header (it's a bandaid solution for sure, but I have yet to find bugs or similar problems with other controls like tab items)
[search result before]
Not all bindings found
[search result after]
As far as I can tell, all bindings found
[Modified recursive method in BindingManager]
internal void FindBindings(DependencyObject parent)
{
int childCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childCount; i++)
{
DependencyObject dpObject = VisualTreeHelper.GetChild(parent, i);
FrameworkElement child = dpObject as FrameworkElement;
if (child != null)
{
string childTag = child.Tag?.ToString();
if (childTag != null && staticReferenceBindings.ContainsKey(childTag))
{
if (staticReferenceBindings[childTag] == null) staticReferenceBindings[childTag] = new List<FrameworkElement>();
staticReferenceBindings[childTag].Add(child);
}
}
//New condition here
if (child is TabItem && ((TabItem)child).Content != null)
{
DependencyObject tabContent = ((TabItem)child).Content as DependencyObject;
FindBindings(tabContent);
}
FindBindings(dpObject);
}
}
if anyone still as anything to add to this or a more universal solution pls don't hesitate to comment.

Related

Accessing control from datatemplate inside of gridview

I have a ListView that displays a table with various columns. Each cell of a row in the table contains a different type of control; I am trying to allow the user to edit the data in each row by selecting a row and double-clicking it to make the cells editable. So I have been able to get all of them to work with the exception of the column that contains ComboBoxes.
XAML code:
This is the XAML code for the ListView. It has about 7 columns but I am focusing on the column with ComboBoxes as depicted here.
<ListView x:Name="MyListView" IsSynchronizedWithCurrentItem="True" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,12,0,0" Height="315" Width="560" ItemsSource="{Binding People}">
<ListView.View>
<GridView>
<!-- More Grid column code here -->
<GridViewColumn Header="Fleet" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="locationCmboBx" ItemsSource="{Binding DataContext.SchoolLocations, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}" Loaded="OnCmboBxLoad" IsEnabled="False" Width="55" HorizontalAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<!-- More Grid column code here -->
</GridView>
</ListView.View>
</ListView>
C# code:
So here in the code-behind I am trying to use the VisualTreeHelper as recommended by others to get access to the locationsCmboBx (ComboBox) nested inside of the DataTemplate, CellTemplate and other XAML headers in the ListView.
// More code before here
ListView listViewItem = (ListView)(MyListView.ItemContainerGenerator.ContainerFromItem(MyListView));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(listViewItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
ComboBox comboBox = (ComboBox)myDataTemplate.FindName("locationsCmboBx", myContentPresenter);
// More code before here
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;
}
So everything I have works but when I debug through the code and get to the FindName function ComboBox is null. Ultimately, I want to set the IsEnabled property on it and get the SelectedValue from the locationsCmboBx. I believe I am missing something but not sure of what. Any help would be appreciated?
The problem in your code is a typo by combo box's name in XAML locationCmboBx and in code behind is locationsCmboBx.
The code:
ListView listViewItem = (ListView)(MyListView.ItemContainerGenerator.ContainerFromItem(MyListView));
is also wrong. Argument of ContainerFromItem() must be a data item. Returned type is also wrong. It must be ListViewItem
I would recommend you to use a ViewModel + bindings and not a code behind to access the data. So you can avoid such a tipo errors. See also: Detect in XAML broken bindings already at compile time
I found this information:
public static class ListViewHelper
{
public static FrameworkElement GetElementFromCellTemplate(ListView listView, Int32 column, Int32 row, String name)
{
if (row >= listView.Items.Count || row < 0)
{
throw new ArgumentOutOfRangeException('row');
}
GridView gridView = listView.View as GridView;
if (gridView == null) { return null; }
if (column >= gridView.Columns.Count || column < 0)
{
throw new ArgumentOutOfRangeException('column');
}
ListViewItem item = listView.ItemContainerGenerator.ContainerFromItem(listView.Items[row]) as ListViewItem;
if (item != null)
{
GridViewRowPresenter rowPresenter = GetFrameworkElementByName(item);
if (rowPresenter != null)
{
ContentPresenter templatedParent = VisualTreeHelper.GetChild(rowPresenter, column) as ContentPresenter;
DataTemplate dataTemplate = gridView.Columns[column].CellTemplate;
if (dataTemplate != null && templatedParent != null)
{
return dataTemplate.FindName(name, templatedParent) as FrameworkElement;
}
}
}
return null;
}
private static T GetFrameworkElementByName(FrameworkElement referenceElement) where T : FrameworkElement
{
FrameworkElement child = null;
for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceElement); i++)
{
child = VisualTreeHelper.GetChild(referenceElement, i) as FrameworkElement;
System.Diagnostics.Debug.WriteLine(child);
if (child != null && child.GetType() == typeof(T))
{
break;
}
else if (child != null)
{
child = GetFrameworkElementByName(child);
if (child != null && child.GetType() == typeof(T))
{
break;
}
}
}
return child as T;
}
}
Source: How do I access the ui element at a row/cell in my GridView?

Two-way-binding: editing passed value from XAML control in the model setter does not update control

This is for a Windows 10 Universal App.
XAML:
<RelativePanel Padding="4" Margin="4,12,0,0">
<TextBlock x:Name="Label" Text="Class Name" Margin="12,0,0,4"/>
<ListView x:Name="ClassTextBoxes"
ItemsSource="{Binding TextBoxList}"
SelectionMode="None" RelativePanel.Below="Label">
<ListView.ItemTemplate>
<DataTemplate >
<RelativePanel>
<TextBox x:Name="tbox"
PlaceholderText="{Binding PlaceHolder}"
Text="{Binding BoxText,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Padding="4" Width="200" MaxLength="25"/>
<TextBlock x:Name="errorLabel"
RelativePanel.Below="tbox"
Text="{Binding Error, Mode=TwoWay}"
Padding="0,0,0,4"
FontSize="10"
Foreground="Red"/>
<Button Content="Delete" Margin="12,0,0,0" RelativePanel.RightOf="tbox"/>
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</RelativePanel>
Model:
public class TextBoxStrings : BaseModel
{
private string _placeholder;
public string PlaceHolder
{
get { return _placeholder; }
set
{
if (_placeholder != value)
{
_placeholder = value;
NotifyPropertyChanged();
}
}
}
private string _boxText;
public string BoxText
{
get { return _boxText; }
set
{
if (_boxText != value)
{
_boxText = CheckBoxText(value);
NotifyPropertyChanged();
}
}
}
public string CheckBoxText(string val)
{
var r = new Regex("[^a-zA-Z0-9]+");
return r.Replace(val, "");
}
}
ViewModel:
private TrulyObservableCollection<TextBoxStrings> _textBoxList;
public TrulyObservableCollection<TextBoxStrings> TextBoxList
{
get { return _textBoxList; }
set
{
if (_textBoxList != value)
{
_textBoxList = value;
RaisePropertyChanged();
}
}
}
and I add new TextBoxString objects to my TextBoxList collection from within my view-model.
I want to make it that users can't type in certain characters (or rather, they get deleted whenever they
are typed in.
This works...in the model. Setting breakpoints and looking at the values, everything in the Model is working: value goes into the setter and gets changed, _boxText holds the new value that is set from CheckBoxText();
But the problem is, in my View, the textbox doesn't reflect changes to the underlying text that I make in the model.
So if I type in "abc*()" into "tbox", the value in the model will be "abc". The value of the textbox, however, will still be "abc*()".
I have a feeling it has something to do with the fact that I'm editing items that are inside of a collection and I don't have anything implemented to handle changing items within a collection. I was under the impression that using INotifyPropertyChanged and ObservableCollection<T> would take care of that for me.
Does anyone have any suggestions?
Thank you!
Edit: So, now I'm trying to use TrulyObservableCollection because I thought this was the problem, but it hasn't helped. Here it is: https://gist.github.com/itajaja/7507120
But the problem is, in my View, the textbox doesn't reflect changes to the underlying text that I make in the model.
As you've seen, the TextBox do reflect changes to your model. When you type in "abc*()" in the TextBox, the value in the model will be changed to "abc". The problem here is that the binding system in UWP is "intelligent". For TwoWay bindings, changes to the target will automatically propagate to the source and in this scenario, binding system assumes that the PropertyChanged event will fire for corresponding property in source and it ignores these events. So even you have RaisePropertyChanged or NotifyPropertyChanged in you source, the TextBox still won't update.
In WPF, we can call BindingExpression.UpdateTarget Method to force the update. But this method is not available in UWP.
As a workaround, you should be able to use TextBox.TextChanged event to check the input like following:
private void tbox_TextChanged(object sender, TextChangedEventArgs e)
{
var tb = sender as TextBox;
if (tb != null)
{
var originalText = tb.Text;
var r = new Regex("[^a-zA-Z0-9]+");
if (originalText != r.Replace(originalText, ""))
{
var index = (tb.SelectionStart - 1) < 0 ? 0 : (tb.SelectionStart - 1);
tb.Text = r.Replace(originalText, "");
tb.SelectionStart = index;
}
}
}
However it may break your MVVM model, you can use data validation to avoid this and here is a blog: Let’s Code! Handling validation in your Windows Store app (WinRT-XAML) you can refer to. And for my personal opinion, data validation is a better direction for this scenario.
if (_boxText != value)
{
_boxText = CheckBoxText(value);
NotifyPropertyChanged();
}
Try changing this to:
var tmp = CheckBoxText(value);
if (_boxText != tmp)
{
_boxText = tmp;
NotifyPropertyChanged();
}
I hope, in your XAML, the binding to property BoxText is two-way, right?
You should edit BoxText and then send checked value to UI. Just send value to CheckBoxText and already edited should be assigned to _boxText. And then you should send BoxText to UI by calling RaisePropertyChanged("BoxTest"). Please, see the following code snippet:
private string _boxText;
public string BoxText
{
get { return _boxText; }
set
{
if (_boxText != value)
{
_boxText=CheckBoxText(value);
RaisePropertyChanged("BoxText");
}
}
}
There is no difference where you use INotifyPropertyChanged for one property of for properties placed in collection. The complete example with collections and ListView can be seen here

c# Editable Text Block not retaining values

I have an EditableTextBlock that has been working for some time in an existing application, however all of a sudden the behaviour of the EditableTextBLock has changed and is no longer retaining the new information on enter.
This is the XAML I am using
DataTemplate x:Key="percentageTemplate" >
<AdornerDecorator>
<platformControls:EditableTextBlock Focusable="True" Validation.ValidationAdornerSite="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}}"
Text="{Binding Path=Percentage, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True}"
KeyboardNavigation.DirectionalNavigation="Continue"
KeyboardNavigation.IsTabStop="True"/>
</AdornerDecorator>
</DataTemplate>
<DataTemplate x:Key="specTemplate">
<AdornerDecorator>
<platformControls:EditableTextBlock Focusable="True" Validation.ValidationAdornerSite="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}}"
Text="{Binding Path=SpecificGravity, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True}"
KeyboardNavigation.DirectionalNavigation="Continue"
KeyboardNavigation.IsTabStop="True"/>
</AdornerDecorator>
</DataTemplate>
The problem as I see it is that is not getting to the setter, but is getting to the getter.
#region Property: SpecificGravity
private double _specificGravity;
public double SpecificGravity
{
get { return this._specificGravity; }
set
{
if (value != 1)
{
IsModified = true;
}
_specificGravity = value;
OnPropertyChanged("SpecificGravity");
}
}
#endregion
When getting to this method below the textblock has the right data, but when the expression.UpdateTarget() runs, the value just returns back to its original value
private static void IsInEditModeUpdate(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
EditableTextBlock textBlock = obj as EditableTextBlock;
if (null != textBlock)
{
//Get the adorner layer of the uielement (here TextBlock)
AdornerLayer layer = AdornerLayer.GetAdornerLayer(textBlock);
//If the IsInEditMode set to true means the user has enabled the edit mode then
//add the adorner to the adorner layer of the TextBlock.
if (textBlock.IsInEditMode)
{
if (null == textBlock._adorner)
{
textBlock._adorner = new EditableTextBlockAdorner(textBlock);
textBlock._adorner.Tag = textBlock;
//Events wired to exit edit mode when the user presses Enter key or leaves the control.
textBlock._adorner.TextBoxKeyUp += textBlock.TextBoxKeyUp;
textBlock._adorner.Focusable = true;
textBlock._adorner.TextBoxLostFocus += textBlock.TextBoxLostFocus;
}
layer.Add(textBlock._adorner);
}
else
{
//Remove the adorner from the adorner layer.
Adorner[] adorners = layer.GetAdorners(textBlock);
if (adorners != null)
{
foreach (Adorner adorner in adorners)
{
if (adorner is EditableTextBlockAdorner)
{
layer.Remove(adorner);
}
}
}
//Update the textblock's text binding.
BindingExpression expression = textBlock.GetBindingExpression(TextProperty);
if (null != expression)
{
expression.UpdateTarget();
}
}
}
}
The code that is used has been in this system for some time and looks like it originally came from here.
Code Project Link
The odd thing is percentageTemplate works as I would expect\want.
Any and all help appreciated.
Thanks

How do I access a TextBlock which is inside my ListBox's DataTemplate (but not Binding) in XAML?

XAML
<ListBox x:Name="lsbQueue" Margin="0,0,0,10" Grid.RowSpan="2" Loaded="lsbQueue_Loaded" SelectionChanged="lsbQueue_SelectionChanged" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="stk" Orientation="Vertical">
<!-- This is the bugger which I need to access behind the scenes-->
<TextBlock x:Name="tbActive" FontSize="35" FontFamily="Segoe UI Symbol" Text="" Height="115" Margin="0,0,0,-110" Tag="Active"/>
<!-- -->
<TextBlock Text="{Binding Path=SongName}" FontSize="35" Width="388" FontWeight="Normal" Margin="60,0,0,0"/>
<TextBlock Width="390" FontWeight="Thin" Margin="60,-5,0,10" Opacity="0.55">
<Run Text="{Binding Artist}" />
<Run Text=", " /> <!-- space -->
<Run Text="{Binding Album}" />
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The above is my Listbox which is the populated from code behind with the help of this:
C#
void GetQueue()
{
var songs = new List<song>();
for (int i = 0; i < MediaPlayer.Queue.Count; i++)
{
songs.Add(new song {
SongName = MediaPlayer.Queue[i].Name.ToString(),
Album = MediaPlayer.Queue[i].Album.Name.ToString(),
Artist = MediaPlayer.Queue[i].Artist.ToString()
});
}
lsbQueue.ItemsSource = songs.ToList();
//lsbQueue.SelectedValue.ToString();
GlobalVars._song = MediaPlayer.Queue.ActiveSongIndex;
lsbQueue.SelectedIndex = GlobalVars._song;
// .......
}
and
public class song
{
public string SongName { get; set; }
public string Album { get; set; }
public string Artist { get; set; }
}
public class Song : List<song>
{
public Song()
{
Add(new song {
SongName = "",
Album = "",
Artist = ""
});
}
}
I have tried using VisualTreeHelper and other extension methods which can be found here:
GeekChamp
Falafel Blog
But I've had no success. I've almost given up on this. Does anyone have any ideas what can be done. Thank you.
As you can see - I can successfully get the Media Queue but I would like to show a visual hint on the left hand side of the "SelectedItem" like the playing character in the TextBlock - tbActive. Hope this helps!
Since the <TextBlock> is the first entry in the DataTemplate that you're trying to access use the provided function from the GeekChamp's tutorial.
<ListBox x:Name="lb" SelectionChanged="lb_SelectionChanged"/>
// namespaces
using System.Windows.Media;
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}
private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// get the ListBoxItem by SelectedIndex OR index number
//ListBoxItem lbi = (ListBoxItem) this.lb.ItemContainerGenerator.ContainerFromIndex(lb.SelectedIndex);
// get the ListBoxItem by SelectedItem or object in your ViewModel
ListBoxItem lbi = (ListBoxItem)this.lb.ItemContainerGenerator.ContainerFromItem(lb.SelectedItem);
// get your textbox that you want
TextBlock tbActive= FindFirstElementInVisualTree<TextBlock>(lbi);
}
The above answer will throw an Exception - just like Chubosaurus Software suggested the SelectedItem will be a 'Song' and thefore the TextBlock will also be a null. And it won't work.
You can try get StackPanel from ListBox's Selected Item using as operator and then use Children property with indexer to get to your TextBlock.
StackPanel temp = lsbQueue.SelectedItem as StackPanel;
var textBlock = temp.Children[0] as TextBlock;
What exactly do you want to accomplish? Maybe another Binding + possible ValueConverter would be way better solution...

Windows Store, Accessing Canvas from code stored in DataTemplate of GridView item

How can I access a Canvas control, stored in DataTemplate of GridView items from my C# code?
<DataTemplate x:Key="250x250ItemTemplate">
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
<Canvas x:Name="Canv"/> <------ I WANT ACCESS THIS CANVAS FROM C# CODE
</Border>
</Grid>
</DataTemplate>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<GridView x:Name="GridViewData" ItemTemplate="{StaticResource 250x250ItemTemplate}"/>
</Grid>
I'm filling GridViewData items from C# code, setting GridViewData.ItemsSource with data from remotely loaded XML.
Then I need to modify Canvas (by adding children to it) of each element separately.
But I don't understand how can I do that.
Can anyone help me with it?
Thank you in advance!
Everyone who interested in answering this question!
I've found a solution here: http://www.wiredprairie.us/blog/index.php/archives/1730
It's horrible that I don't understand why we need to do so much magic here, but it works.
namespace Extension
{
public static class FrameworkElementExtensions
{
public static FrameworkElement FindDescendantByName(this FrameworkElement element, string name)
{
if (element == null || string.IsNullOrWhiteSpace(name))
{
return null;
}
if (name.Equals(element.Name, StringComparison.OrdinalIgnoreCase))
{
return element;
}
var childCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childCount; i++)
{
var result = (VisualTreeHelper.GetChild(element, i) as FrameworkElement).FindDescendantByName(name);
if (result != null)
{
return result;
}
}
return null;
}
}
}
and
for (int i = 0; i<GridViewTickers.Items.Count; i++)
{
var element = GridViewTickers.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement;
if (element != null)
{
var tb = element.FindDescendantByName("Canv") as Canvas;
if (tb != null)
{
TextBlock tb1 = new TextBlock();
tb1.Text = "hello";
tb.Children.Add(tb1);
}
}
}
If anyone can explain for me what we're doing in this bunch of code - please do this, 'cause my brain is exploding right now :)
Thank you all!

Categories

Resources