my xaml code..
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="DataTemplate1" >
<Border BorderBrush="LightGray" BorderThickness="1" Height="150" Width="500" >
<Grid Width="500" Height="150" Background="White" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.9*"/>
<ColumnDefinition Width="2.5*"/>
<ColumnDefinition Width="1.5*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Name="imgitem" Grid.Column="0" Height="Auto" Width="Auto" Source="{Binding ImgSource}" Margin="0,5,4,4" HorizontalAlignment="Left" />
<TextBlock x:Name="txtbindprice" Text="{Binding _PRICE,ConverterCulture=en-IN,StringFormat=C}" TextWrapping="Wrap" Grid.Column="1" Width="350" Foreground="Black" Height="60" Margin="40,70,20,-10"/>
<TextBlock x:Name="txtFinalTotal" Text="{Binding _FinalTotal}" TextWrapping="Wrap" Grid.Column="1" Width="350" Foreground="Red" Height="60" Margin="40,115,20,-10"/>
<TextBlock x:Name="txtITMNAME" Text="{Binding _ITMNAME }" Padding="1" Tap="ItmName_Tapped" TextDecorations="Underline" FontSize="24" TextWrapping="Wrap" Grid.Column="1" FontWeight="Normal" TextTrimming="WordEllipsis" Foreground="OrangeRed" Width="Auto" Height="150" Margin="30,25,10,-10"/>
<CheckBox Grid.Column="2" Grid.Row="0" Background="Black" Foreground="Black" BorderThickness="4" BorderBrush="Red" Margin="10,20,-15,10" Checked="CheckBox_Checked" Unchecked="CheckBox_UnChecked" />
</Grid>
</Border>
</DataTemplate>
<ListBox Height="Auto" Name="lstbxmanual" SelectionMode="Extended" ItemTemplate="{StaticResource DataTemplate1 }" Width="475" Margin="2,192,0,-39" Background="White" HorizontalAlignment="Left" Grid.RowSpan="2">
</ListBox>
I want to access textblocks inside listbox based on selected index
accessing textblock
string a =txtbindprice.text
wont work as they are inside data template of listbox..
I came across visual child tree methods and some other examples..i cant find much info..
please help me regarding this...
The following method finds currently selected list-box item:
private ListBoxItem FindSelectedListBoxItem(DependencyObject control)
{
int childNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < childNumber; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(control, i);
FrameworkElement fe = child as FrameworkElement;
// Not a framework element or is null
if (fe == null) return null;
if (child is ListBoxItem && ((ListBoxItem)child).IsSelected)
{
// Found the control so return
return (ListBoxItem)child;
}
else
{
// Not found it - search children
ListBoxItem nextLevel = FindSelectedListBoxItem(child);
if (nextLevel != null)
return nextLevel;
}
}
return null;
}
Below method finds any child control with a certain name from a given root control:
private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
int childNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < childNumber; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(control, i);
FrameworkElement fe = child as FrameworkElement;
// Not a framework element or is null
if (fe == null) return null;
if (child is T && fe.Name == ctrlName)
{
// Found the control so return
return child;
}
else
{
// Not found it - search children
DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
if (nextLevel != null)
return nextLevel;
}
}
return null;
}
Now you can use the first method to get currently selected ListBoxItemand then pass it to the second method to retrieve any control inside this ListBoxItem (e.g. txtbindprice):
ListBoxItem item = FindSelectedListBoxItem(this);
TextBlock txtPrice = FindChildControl<TextBlock>(item , "txtbindprice") as TextBlock;
string a = txtPrice.Text;
Update
You can add SelectionChanged event to your ListBox like this:
<ListBox Height="Auto" Name="lstbxmanual" SelectionChanged="OnSelectionChanged">
</ListBox>
And then retrieve a certain TextBlock.Text (e.g. txtbindprice) this way:
public void OnSelectionChanged(object sender, SelectionChangedEventArgs args)
{
ListBoxItem item = FindSelectedListBoxItem((ListBox(sender)));
TextBlock txtPrice = FindChildControl<TextBlock>(item , "txtbindprice") as TextBlock;
string a = txtPrice.Text;
}
Related
Does anybody know how I can retrieve the selected Ellipse "bgColor" from the following code please?
I can retrieve the Color, but I need to get the Ellipse which holds it.
XAML
<ListView Name="BgColorList" Height="80" Width="850"
ItemsSource="{Binding ColorList}"
ItemContainerStyle="{StaticResource ListViewTransparent}"
ScrollViewer.HorizontalScrollMode="Enabled"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.IsHorizontalRailEnabled="True"
IsItemClickEnabled="True"
VerticalAlignment="Bottom"
SelectionChanged="BgColorList_SelectionChanged"
Margin="0,0,0,35">
<ListView.DataContext>
<local2:NamedColors />
</ListView.DataContext>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Grid VerticalAlignment="Center" Margin="0,0,0,0" Height="65" Width="65">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Ellipse Name="bgColor" Grid.Column="0" Height="50" Width="50" Margin="2" VerticalAlignment="Center" Stroke="Transparent" StrokeThickness="1">
<Ellipse.Fill>
<SolidColorBrush Color="{Binding }" />
</Ellipse.Fill>
</Ellipse>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C# - I don't seem to be able to find a way to retrieve the Ellipse here.
private void BgColorList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView view = (ListView)sender;
var selected = view.SelectedItem;
Color selectedColor = (Color)view.SelectedItem;
}
The ListView has a ContainerFromItem methid which should give you a reference to the ListViewItem container. You could then get a reference to the Ellipse using the VisualTreeHelper class:
private void BgColorList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView view = (ListView)sender;
var selected = view.SelectedItem;
var container = view.ContainerFromItem(selected);
if (container != null)
{
Ellipse ellipse = FindVisualChild<Ellipse>(container);
if (ellipse != null)
{
//...
}
}
}
private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
How to get the Parent Item of the SelectedItem on ListView.SelectionChanged
For ListView, you could use ContainerFromItem method to get ListViewItem for SelectedItem, then use VisualTreeHelper to find the child element, but VisualTreeHelper will consume system performance. And for you scenario, the better way is listen Ellipse Tapped event as #Noorul said, when the item is clicked the Tapped even will invoke.
private void BgColor_Tapped(object sender, TappedRoutedEventArgs e)
{
var ellipse = sender as Ellipse;
}
I have this code:
<HubSection x:Name="MyHub">
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="200"></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="MYImage" Source="{Binding image}" Height="50" Width="60" VerticalAlignment="Center"/>
<StackPanel Orientation="Vertical">
<TextBlock TextWrapping="Wrap" Width="200" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding name}"/>
<Button Content="Click me">
</StackPanel>
</Grid>
</DataTemplate>
</HubSection>
But I do not know how to access the text-block, I want to change the foreground color of the text-block in code behind.
XAML:
<Hub x:Name="myHub">
<HubSection x:Name="myHubSection">
<DataTemplate>
<TextBlock x:Name="textbox1" Text="text" Width="300" Height="100">
</TextBlock>
</DataTemplate>
</HubSection>
</Hub>
Code behind:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var hub_section = FindVisualChildByName<HubSection>(this.myHub, "myHubSection");
var text_box = FindVisualChildByName<WebView>(hub_section, "textbox1");
}
public static T FindVisualChildByName<T>(DependencyObject parent, string name)where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
string controlName = child.GetValue(Control.NameProperty) as string;
if (controlName == name)
{
return child as T;
}
else
{
T result = FindVisualChildByName<T>(child, name);
if (result != null)
return result;
}
}
return null;
}
More detailed information you could refer to How to access a Control inside the data template in C# Metro UI in the code behind
<ListView Height="Auto" Name="lstIndent" SelectionMode="Single" Grid.ColumnSpan="5" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0" >
<ListView.View >
<GridView x:Name="dgIndentDetails" >
<GridViewColumn Width="Auto" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<!--<GroupBox Header="Department Issue Header" Grid.Row="5" Grid.ColumnSpan="5" HorizontalAlignment="Stretch" >-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" Grid.Column="0" IsChecked="{Binding Path=IsIndentIssue, UpdateSourceTrigger=PropertyChanged}" x:Name="chkbxIsChecked" HorizontalAlignment="Center"/>
<Label Grid.Column="1" Grid.Row="0" Content="{Binding ItemName}" x:Name="lbllstItemName" HorizontalAlignment="Center" ></Label>
<Label Grid.Column="2" Grid.Row="0" Content="{Binding Quantity}" x:Name="lbllstQty" HorizontalAlignment="Center" ></Label>
<Label Grid.Column="3" Grid.Row="0" Content="{Binding IssueQuantity}" x:Name="lbllstIssuedQty" HorizontalAlignment="Center" ></Label>
<DataGrid Height="Auto" Padding="10,10,10,10" Width="800" Grid.Row="1" Grid.ColumnSpan="5" Name="dgIssuedItemsBatchDetails" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ItemName}">
<DataGridTextColumn.Header>
<TextBlock Text="ItemName"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding BatchNo}">
<DataGridTextColumn.Header>
<TextBlock Text="BatchNo"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Quantity}">
<DataGridTextColumn.Header>
<TextBlock Text="Quantity"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
<!--</GroupBox>-->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I need to find DataGrid dgIssuedItemsBatchDetails and then bind. That DataGrid is placed inside GridView and that GridView is placed inside ListView.
GridView grdvwIssueDetail = (GridView)lstIndent.FindName("dgIndentDetails");
By using this code I am able to find the GridView. My DataGrid is placed inside GridView DataTemplate. So suggest me how I can find my DataGrid from code behind.
It can be achieved by using VisualTreeHelper.
As MSDN says:
VisualTreeHelper class provides utility methods that perform common tasks involving nodes in a visual tree.
So let me show an example how it can be achieved:
private void SeeTheChild()
{
DataGrid myCombo = GetVisualChildInDataTemplate<DataGrid>(lstIndent);
}
private T GetVisualChildInDataTemplate<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 = GetVisualChildInDataTemplate<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
VisualTreeHelper.GetChildrenCount(parent) returns 0 cause ListView is created, but not yet loaded. Particularly, the DataTemplate has not yet been applied to the ListView, consequently there is nothing in the Visual Tree.
Then use this method to get your DataGrid on Loaded event of your Window:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SeeTheChild();
}
Update:
To find a control per item in ListView:
Create a SelectionChanged event:
private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ItemContainerGenerator generator = this.listView.ItemContainerGenerator;
ListBoxItem selectedItem = (ListBoxItem)generator.ContainerFromIndex(listView.SelectedIndex);
Label aLabel = GetChildrenByType(selectedItem, typeof(Label), "label") as Label;
if (aLabel != null)
{
MessageBox.Show("We've found Label with name 'label': " + aLabel.Content);
}
else
{
MessageBox.Show("There is no such Label");
}
}
and a method which can find a necessary control for you:
public static Visual GetChildrenByType(Visual visualElement, Type typeElement, string nameElement)
{
if (visualElement == null) return null;
if (visualElement.GetType() == typeElement)
{
FrameworkElement fe = visualElement as FrameworkElement;
if (fe != null)
{
if (fe.Name == nameElement)
{
return fe;
}
}
}
Visual foundElement = null;
if (visualElement is FrameworkElement)
(visualElement as FrameworkElement).ApplyTemplate();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualElement); i++)
{
Visual visual = VisualTreeHelper.GetChild(visualElement, i) as Visual;
foundElement = GetChildrenByType(visual, typeElement, nameElement);
if (foundElement != null)
break;
}
return foundElement;
}
I am trying to find a control under my xaml in the hub control of windows phone 8.1. It may be the case my method works fine on desktop as I have testest it in wpf before but I am porting it to windows phone and may not work the same.
<Grid>
<Hub Header="Lists" Name="mainHub" >
<HubSection MinWidth="600" Name="lattestLists" Header="New Lists">
<DataTemplate>
<Grid>
<ListBox Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" x:Name="listBoxobj" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="350" >
<Border Margin="5" BorderBrush="White" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Margin="5,0,0,0" Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" FontSize="28" Foreground="White"/>
<TextBlock Grid.Row="0" Text=">" FontSize="28" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="White"/>
<TextBlock Margin="5,0,0,0" Grid.Row="1" x:Name="PhoneTxt" TextWrapping="Wrap" Foreground="White" FontSize="18" Text="{Binding PhoneNumber}" />
<TextBlock HorizontalAlignment="Right" Margin="0,0,35,0" Grid.Row="3" x:Name="CreateddateTxt" Foreground="White" FontSize="18" TextWrapping="Wrap" Text="{Binding CreationDate}" />
</Grid>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</HubSection>
<HubSection Header="Tech" IsHeaderInteractive="True"
Background="#222222" MinWidth="250">
<DataTemplate>
<StackPanel>
<TextBlock Text="Tech news goes here."
Style="{ThemeResource BodyTextBlockStyle}" />
<TextBlock Text="Click the header to go to the Tech page."
Style="{ThemeResource BodyTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</HubSection>
<HubSection Header="Sports" IsHeaderInteractive="True"
Background="#444444" MinWidth="250">
<DataTemplate>
<StackPanel>
<TextBlock Text="Sports news goes here."
Style="{ThemeResource BodyTextBlockStyle}" />
<TextBlock Text="Click the header to go to the Sports page."
Style="{ThemeResource BodyTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</HubSection>
</Hub>
</Grid>
I am trying to use the following method to find the lsitbox but its not working
ListBox listBoxobjc = FindChildControl<ListBox>(this, "listBoxobj") as ListBox;
listBoxobjc.ItemsSource = DB_ContactList.OrderByDescending(i => i.id).ToList();//Binding DB data to LISTBOX and Latest contact ID can Display first.
This is the method FindChildControl
private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
int childNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < childNumber; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(control, i);
FrameworkElement fe = child as FrameworkElement;
// Not a framework element or is null
if (fe == null) return null;
if (child is T && fe.Name == ctrlName)
{
// Found the control so return
return child;
}
else
{
// Not found it - search children
DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
if (nextLevel != null)
return nextLevel;
}
}
return null;
}
Edit
I debuged the code and its showing the control as null even though I have done the same way as i would
I have replace my method to find children a couple of times during the different versions of Windows Phone / Windows Runtime with this one being the most reliable. Just in case you're not able to spot the mistake you could also try this one and see if it yields better results:
public T FindElementByName<T>(DependencyObject element, string sChildName) where T : FrameworkElement
{
T childElement = null;
var nChildCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < nChildCount; i++)
{
FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;
if (child == null)
continue;
if (child is T && child.Name.Equals(sChildName))
{
childElement = (T)child;
break;
}
childElement = FindElementByName<T>(child, sChildName);
if (childElement != null)
break;
}
return childElement;
}
Also when using this method shortly after creating / loading content I call UpdateLayout beforehand:
this.UpdateLayout();
But be careful - this is not one of the performance-friendliest methods (see also: https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.updatelayout).
I have a ListBox with the following XAML:
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Name="listItemGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="20" MinWidth="20" Width="20" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Rectangle Name="listItemSideBar" Height="85" HorizontalAlignment="Left"
Margin="0, 0, 0, 0" Stroke="{StaticResource PhoneAccentBrush}"
StrokeThickness="1" VerticalAlignment="Top" Fill="{StaticResource
PhoneAccentBrush}" MinHeight="85" Width="25"/>
<StackPanel Margin="0,0,0,17" Grid.Column="1" Grid.ColumnSpan="2">
<TextBlock Name="listItemMainData" Text="{Binding LineTwo}"
TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource
PhoneTextExtraLargeStyle}"/>
<TextBlock Name="listItemSubData" Text="{Binding LineOne}"
TextWrapping="NoWrap" Margin="12,-6,0,0"
Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
When a ListBoxItem is selected I would like to change that ListBoxItem's Rectangle fill color to a different color; when the ListBoxItem is deselected I would like the fill color to change back to PhoneAccentBrush.
Is there a way to accomplish this task?
I finally worked out a solution which I have copied below. The ColorHelper class simply gets the PhoneAccentColor from the PhoneAccentBrush resource. The DarkPhoneAccentColor is produced via the method found here. PropertyValue<string>(child, "Name") is a syntactic sugar extension method to get a value from a property of a type; it returns default(T) if the property does not exist.
Full error checking has not yet been applied to this code.
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Color accent = ColorHelper.PhoneAccentColor;
Color accentDark = ColorHelper.DarkPhoneAccentColor;
foreach (object item in e.RemovedItems)
SetListBoxItemColor(item, accentDark);
foreach (object item in e.AddedItems)
SetListBoxItemColor(item, accent);
}
private void SetListBoxItemColor(object item, Color color)
{
ListBoxItem listBoxItem = listBox.ItemContainerGenerator
.ContainerFromItem(item) as ListBoxItem;
if (listBoxItem != null)
{
Rectangle rectangle = GetItemsRecursive<Rectangle>(
listBoxItem, "listItemSideBar");
SolidColorBrush fillBrush = new SolidColorBrush();
fillBrush.Color = color;
rectangle.Fill = fillBrush;
}
}
private T GetItemsRecursive<T>(DependencyObject lb, string name)
where T : DependencyObject
{
int childrenCount = VisualTreeHelper.GetChildrenCount(lb);
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(lb, i);
if (child is T)
{
string childName = child.GetType().PropertyValue<string>(child, "Name");
if (String.Compare(childName, name) == 0)
return (T)child;
}
return GetItemsRecursive<T>(child, name);
}
return default(T);
}