I need to generate a listview with data I have in an array
my XAML is:
<Page.Resources>
<DataTemplate x:Key="IconTextDataTemplate">
<StackPanel Orientation="Horizontal" Width="220" Height="60">
<Border Background="#66727272" Width="40" Height="40" Margin="10">
<Image Source="/SampleImage.png" Height="32" Width="32" Stretch="UniformToFill"/>
</Border>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<TextBlock x:Name="Name" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis" Loaded="Name_Loaded"/>
<TextBlock x:Name="Description" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis" Loaded="Description_Loaded"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</Page.Resources>
<ListView x:Name="IconTextGrid" Height="400" ItemTemplate="{StaticResource IconTextDataTemplate}" Grid.Row="4" Margin="40,40,40,10" HorizontalAlignment="Stretch">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="8"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
And the C# code:
private async void SearchBox_QuerySubmitted(SearchBox sender, SearchBoxQuerySubmittedEventArgs args)
{
foreach (var item in results)
{
IconTextGrid.Items.Add("");
nameStr = item.FirstName + " " + item.LastName;
descriptionStr = item.Email;
}
}
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;
}
private void Name_Loaded(object sender, RoutedEventArgs e)
{
TextBlock Name = FindElementByName<TextBlock>(this, "Name");
Name.Text = nameStr;
}
private void Description_Loaded(object sender, RoutedEventArgs e)
{
TextBlock Description = FindElementByName<TextBlock>(this, "Description");
Description.Text = descriptionStr;
}
This code should generate a list view with some items (in the array size) and put in every item the data from the cell in the array.
The problem is that only in one item I see the data from the array and in the other items there is nothing.
Wish for help, thanks
Your code is failing for at least one reason:
foreach (var item in results)
{
IconTextGrid.Items.Add("");
nameStr = item.FirstName + " " + item.LastName;
descriptionStr = item.Email;
}
you appear to be storing nameStr and descriptionStr into fields. But you are only storing the last item in results.
The good news is you can simplify and fix your code all at once. Using
foreach (var item in results)
{
var nameStr = item.FirstName + " " + item.LastName;
var descriptionStr = item.Email;
IconTextGrid.Items.Add(new { Name = nameStr, Description = descriptionStr });
}
That will create anonymous types with Name and Description properties for the ListView elements. You can then use:
<DataTemplate x:Key="IconTextDataTemplate">
<StackPanel ...>
<Image .../>
</Border>
<StackPanel ...>
<TextBlock Text="{Binding Name}" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis"/>
<TextBlock Text="{Binding Description} Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis"/>
</StackPanel>
</StackPanel>
</DataTemplate>
And remove your FindElementByName, Name_Loaded, and Description_Loaded methods.
Related
i want to get in my codebehind the Content of an button that has a grid in it with multiple textboxes.
i had this before Code and this works:
XAML:
<Button Click="btnClick_upload_Data">
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="test1" ></TextBlock>
<TextBlock Text="test2" ></TextBlock>
</StackPanel>
</Button.Content>
</Button>
codebehind:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
string s = ((((sender as Button).Content) as StackPanel).Children[1] as TextBlock).Text;
//…
and this way i got the "test2" im my string variable.
now my XAML has changed a bit
my Question is how do i have to Change my function so i still get "test2" in my string variable 's'
new XAML:
<Button Click="btnClick_upload_Data" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="test1" Grid.Row="0"></TextBlock>
<TextBlock Text="test2" Grid.Row="1"></TextBlock>
</Grid>
</StackPanel>
</Button.Content>
</Button>
new XAML:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
//????
thanks in advance
Try this:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
string s = null;
Button btn = (Button)sender;
StackPanel sp = btn.Content as StackPanel;
if (sp != null && sp.Children.Count > 0)
{
Grid grid = sp.Children[0] as Grid;
if (grid != null && grid.Children.Count > 1)
{
TextBlock textBlock = grid.Children[1] as TextBlock;
if (textBlock != null)
s = textBlock.Text;
}
}
MessageBox.Show(s);
}
XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="WpfApplication1.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="637" Width="1889">
<Grid RenderTransformOrigin="0.505,0.844" Margin="0,0,0,19">
<CheckBox x:Name="cbFilter" Content="CheckBox" HorizontalAlignment="Left" Margin="15,410,0,0" VerticalAlignment="Top" Width="100" Height="25"/>
<ComboBox x:Name="cbFilterMode" HorizontalAlignment="Left" Margin="95,410,0,0" VerticalAlignment="Top" Width="120">
<TextBlock>=</TextBlock>
<TabItem/>
</ComboBox>
<TextBox x:Name="txtFilterValue" HorizontalAlignment="Left" Height="23" Margin="225,409,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<GroupBox x:Name="groupBox" Header="GroupBox" HorizontalAlignment="Left" Margin="225,10,0,0" VerticalAlignment="Top" Width="1110" Height="385">
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="245,105,0,0" VerticalAlignment="Top" Height="230" Width="645"/>
</GroupBox>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="385" Margin="10,10,0,0" VerticalAlignment="Top" Width="230" FontFamily="Segoe UI Black" Grid.Column="10">
<ListBox.ItemTemplate >
<DataTemplate>
<CheckBox x:Name="qqq" Content="{Binding }" IsChecked="{Binding IsChecked}" Click="CheckBox_Click"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
Code behind:
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
var cb = sender as CheckBox;
var item = cb.IsChecked;
listBox.SelectedItem = item;
if (listBox.SelectedIndex==0) return;
string query = "SELECT NON EMPTY { [Measures].[Число Подписки] } ON COLUMNS, NON EMPTY {(";
foreach (string i in listBox.Checked)
{
query += " " + i.ToString() + ".ALLMEMBERS *";
}
query = query.Remove(query.Length - 2);
query += " )} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM [Почта] CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS";
UpdateChart(query);
}
It turns out only to withdraw all at once, and how to do that would be deduced by one (ie. If checkBox == true, then we conclude that measurement of the cube)
If you have a such model class:
public class FooClass
{
public bool IsChecked { get; set; }
}
and populating of ListBox looks like that:
public MainWindow()//***ctor of Window
{
InitializeComponent();
PopulateListView();
}
private void PopulateListView()
{
IList<FooClass> someValues = new List<FooClass>();
for (int i = 0; i < 10; i++)
{
someValues.Add(new FooClass() { IsChecked=true});
}
listBox.ItemsSource = someValues;
}
Then to get all CheckBox'es values from ListBox you can do:
private void qqq_Click(object sender, RoutedEventArgs e)
{
var e1=listBox.Items;
foreach (var c in listBox.ItemsSource)
{
MessageBox.Show(((FooClass)c).IsChecked.ToString());
}
}
I have a ListBox that contains a StackPanel which contains an Image and a TextBlock. I want to access the TextBlock via c# (code-behind) to change it's font manually.
Relevent XAML:
<ListBox x:Name="CategoriesListBox" Margin="0,0,-12,0" ItemsSource="{Binding Categories}" SelectionChanged="CategoriesListBox_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="62">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image x:Name="catImage" Source="{Binding icon}"/>
<TextBlock x:Name="catName" Grid.Column="1" Text="{Binding name_shown}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}"/>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I need to access the "catName" TextBlock for all cells.
try this--->
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;
}
u can use above method in this way--->
ListBoxItem item = this.list.ItemContainerGenerator.ContainerFromIndex(2) as ListBoxItem;
TextBlock txt = FindFirstElementInVisualTree<TextBlock>(item);
txt.Text = "some text";
my image is inside the datatemplate, what I want to do it is change the image if the wp theme is light
<DataTemplate x:Key="citiesItemTemplate">
<StackPanel Grid.Column="1" VerticalAlignment="Top">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Height="50" Tap="ProgLngGropus_Tap" Text="{Binding Name}" FontSize="26" Margin="12,-5,12,6"/>
<ToggleButton Grid.Column="2" x:Name="MyToggleButton" Style="{StaticResource FlipButton}">
<ToggleButton.Content>
<Image Grid.Column="2" Margin="0,-10,-33,0" Height="40" Width="40" x:Name="ArrowDownImg" Source="/Images/appbar.dark.arrow.down.circle.rest.png" />
</ToggleButton.Content>
</ToggleButton>
<TextBlock TextWrapping="Wrap" Text="{Binding Lang}" Grid.Column="0" Grid.Row="1" x:Name="Desc"
Foreground="Orange" Visibility="{Binding ElementName=MyToggleButton,
Path=IsChecked, Converter={StaticResource ValueConverterBoolToVis}}">
</TextBlock>
</Grid>
</StackPanel>
</DataTemplate>
But Im unable to access ArrowDownImg
Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
if (darkBackgroundVisibility != Visibility.Visible)
{
//Error in finding ArrowDownImg
***//ArrowDownImg.Source = "/Images/appbar.light.arrow.down.circle.rest.png"***
}
You could also do
Image arrowDownImg = MyObjectUsingTheDataTemplate.FindVisualChild("ArrowDownImg") as Image;
arrowDownImg.Source = ...
by using the following extension methods
public static FrameworkElement FindVisualChild(this FrameworkElement root, string name)
{
FrameworkElement temp = root.FindName(name) as FrameworkElement;
if (temp != null)
return temp;
foreach (FrameworkElement element in root.GetVisualDescendents())
{
temp = element.FindName(name) as FrameworkElement;
if (temp != null)
return temp;
}
return null;
}
public static IEnumerable<FrameworkElement> GetVisualDescendents(this FrameworkElement root)
{
Queue<IEnumerable<FrameworkElement>> toDo = new Queue<IEnumerable<FrameworkElement>>();
toDo.Enqueue(root.GetVisualChildren());
while (toDo.Count > 0)
{
IEnumerable<FrameworkElement> children = toDo.Dequeue();
foreach (FrameworkElement child in children)
{
yield return child;
toDo.Enqueue(child.GetVisualChildren());
}
}
}
public static IEnumerable<FrameworkElement> GetVisualChildren(this FrameworkElement root)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
{
yield return VisualTreeHelper.GetChild(root, i) as FrameworkElement;
}
}
Answer: Here is what I did
I have added the loaded event
<ToggleButton Grid.Row="0" Grid.Column="2" Margin="0,-10,-33,0" x:Name="MyToggleButton" Style="{StaticResource FlipButton}">
<ToggleButton.Content>
<Image Loaded="ArrowDownImg_Loaded" Height="50" Width="50" x:Name="ArrowDownImg" />
</ToggleButton.Content>
</ToggleButton>
and in the event:
private void ArrowDownImg_Loaded(object sender, RoutedEventArgs e)
{
Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
var ArrowDownImg = sender as Image;
if (darkBackgroundVisibility != Visibility.Visible)
{
ArrowDownImg.Source = new BitmapImage(new Uri("/Images/appbar.arrow.down.circle.dark.rest.png", UriKind.Relative));
}
else
{
ArrowDownImg.Source = new BitmapImage(new Uri("/Images/appbar.arrow.down.circle.light.rest.png", UriKind.Relative));
}
}
I have some problems finding the right TextBlock control inside a StackPanel.
My markup:
<ListBox Name="lstTimeline" ItemContainerStyle="{StaticResource TwItemStyle}"
MouseDoubleClick="lstTimeline_MouseDoubleClick">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel MaxWidth="{Binding ElementName=lstTimeline, Path=ActualWidth}">
<Border Margin="10" DockPanel.Dock="Left" BorderBrush="White"
BorderThickness="1" Height="48" Width="48" HorizontalAlignment="Center">
<Image Source="{Binding ThumbNail, IsAsync=True}" Height="48" Width="48" />
</Border>
<StackPanel Name="stkPanel" Margin="10" DockPanel.Dock="Right">
<TextBlock Text="{Binding UserName}" FontWeight="Bold" FontSize="18" />
<TextBlock Text="{Binding Text}" Margin="0,4,0,0" FontSize="14"
Foreground="#c6de96" TextWrapping="WrapWithOverflow" />
<TextBlock Text="{Binding ApproximateTime}" FontSize="14"
FontFamily="Georgia" FontStyle="Italic" Foreground="#BBB" />
<TextBlock Text="{Binding ScreenName}" Name="lblScreenName" FontSize="14"
FontFamily="Georgia" FontStyle="Italic" Foreground="#BBB"
Loaded="lblScreenName_Loaded" />
</StackPanel>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My double click code:
private void lstTimeline_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ListBoxItem lbi = (lstTimeline.SelectedItem as ListBoxItem);
StackPanel item = lbi.FindName("stkPanel") as StackPanel;
if (item != null)
MessageBox.Show("StackPanel null");
TextBlock textBox = item.FindName("lblScreenName") as TextBlock;
if (textBox != null)
MessageBox.Show("TextBlock null");
MessageBox.Show(textBox.Text);
}
But the StackPanel is null. How do find the right TextBlock in SelectedItem?
Thanks for your help.
ListBoxItem myListBoxItem = (ListBoxItem)(lstUniqueIds.ItemContainerGenerator.ContainerFromIndex(lstUniqueIds.SelectedIndex));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
CheckBox target = (CheckBox)myDataTemplate.FindName("chkUniqueId", myContentPresenter);
if (target.IsChecked)
{
target.IsChecked = false;
}
else
{
target.IsChecked = true;
}
Function FindVisualChild can be found on the MSDN page FrameworkTemplate.FindName Method:
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;
}
There's a specific function to use when you're looking for something whose name is defined in a template. Try it like this:
private void lstTimeline_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ListBoxItem lbi = (lstTimeline.SelectedItem as ListBoxItem);
StackPanel item = Template.FindName("stkPanel",lbi) as StackPanel;
if (item != null)
MessageBox.Show("StackPanel null");
TextBlock textBox = Template.FindName("lblScreenName",item) as TextBlock;
if (textBox != null)
MessageBox.Show("TextBlock null");
MessageBox.Show(textBox.Text);
}
Linq to xml with a get and set model.
var item = ...
lstTimeline.SelectedIndex = -1;
lstTimeline.ItemsSource = item;