c# find texbox in WinPhone7 silverlight app - c#

I have something like this:
<ListBox Height="456" Margin="30,113,0,0" x:Name="listBox1" Width="446" Background="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28" Orientation="Horizontal">
<TextBlock Text="{Binding name}" FontSize="28" Padding="10" >
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
Hold="GestureListenerHold" />
</toolkit:GestureService.GestureListener>
</TextBlock>
<TextBlock Text="{Binding id}" FontSize="24" Padding="10" >
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
Hold="GestureListenerHold" />
</toolkit:GestureService.GestureListener>
</TextBlock>
<TextBlock Text="{Binding status}" FontSize="24" Padding="10">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
Hold="GestureListenerHold" />
</toolkit:GestureService.GestureListener>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and in my app i do:
data = (List<Device>)serializer.Deserialize(stream);
this.listBox1.ItemsSource = data;
on every textblock I have a gesture listener which should provide the user with the option to change 'name', so when they hold the textblock the app navigates him to another page where he fills in the form.
My question is how to find the texblock which is binding 'name' when I click and hold another texblock?

You could use Linq-to-VisualTree, a utility which I wrote which allows you to navigate the visual tree:
http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/
Firstly, name the TextBlock so that it can be uniquely identified:
<TextBlock x:Name="NameText" Text="{Binding name}" FontSize="28" Padding="10" />
Then, when one of your other TextBlocks is tapped, you can find it as follows:
// locate the parent stackpanel
var parentStackPanel = tappedTextBlock.Ancestors().First()
// locate the names TextBlock
var nameTextBlock = parentStackPanel.Elements()
.Where(el => el.Name == "NameText").Single();

Cast the sender in the GestureListenerHold method.

Related

How do I refer to a control in a WPF nested ListView?

My XAML:
<ListView x:Name="lvAlbums" ItemsSource="{Binding XPath=/downloads/Album}" SelectionMode="Multiple">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="spAlbum">
<!-- Displays first level attributes -->
<TextBlock x:Name="AlbumName" Text="{Binding XPath=#Name}"/>
<TextBlock x:Name="AlbumArtist" Text="{Binding XPath=#Artist}"/>
<ListView x:Name="lvTracks" ItemsSource="{Binding XPath=Item}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="spTrack">
<!-- Displays the second level attributes -->
<TextBlock x:Name="trackName" Text="{Binding XPath=#Name}"/>
<ProgressBar x:Name="pb1" Minimum="0" Maximum="100" Height="5px" Margin="0,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
It populates just fine from an xml file. I have an album and listed underneath are the tracks and a progress bar for each track.
I am now looping though the XML to download each track. How do I refer to the progressbar?
(ProgressBar)lvAlbums[curAlbum].lvTracks[curTrack].spTrack["pb1"]
Something like that.
Please see if this works for you :
var innerListView = lvAlbums.ItemTemplate.FindName("lvTracks", lvAlbums) as ListView;
ProgressBar pbar = innerListView.ItemTemplate.FindName("pb1", innerListView ) as ProgressBar;
Instead of trying to reference the progressbar control, I simply bound data to it and updated the data:
<ProgressBar x:Name="pb1" Value="{Binding XPath=#Progress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Minimum="0" Maximum="100" Height="5px" Margin="0,0,0,0" Width="145"/>
Works as expected.

How to access checkbox value from a listbox in windows phone

I have 10 checkboxes in a listbox in windows phone.
My xaml file for checkbox into listbox is given below:
<ListBox x:Name="notificationSettingsListBox" Grid.Row="1" Margin="20,20,20,20" Background="#e79e38" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="#055cc3" Width="500" Height="200">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding channel_name}" Foreground="White" FontSize="31" TextAlignment="Left" TextWrapping="Wrap" />
<CheckBox Content="Enable Notification" Checked="pushNotiOnCheckBox_Checked" Unchecked="pushNotiOnCheckBox_Unchecked"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How can I access all checkboxes one by one like checkbox1.checked=true or checkbox2.checked=false and so on?
I see that you subscribe to both Checked and Unchecked events, one thing you could do is add this in the handlers
var box = (sender as CheckBox);
var value = box.IsChecked;

How to show a list of items?

I need to show a list of tweets. each tweet can be highlighted programmatically and also I can add some new controls to the tweet item. and I am using data binding.
for my purpose above, I thought of using a LongListSelector but it is too buggy, and it seems that I can't access a collection of its items (can I?). what else is suitable for showing a list of items, each item can contain some other controls, and also supports binding? I don't see another thing in the toolbox? is there any ester egg?
It's windows phone 8 app.
You can use a ListBox
<ListBox Name="listboxTimeline" Width="450" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,5,0,5" >
<Button Click="btn1_Click" >
<StackPanel VerticalAlignment="Top">
<Image Name="image" Source="{Binding ImageSource}" />
</StackPanel>
</Button>
<StackPanel Width="337">
<TextBlock Text="{Binding UserName}" />
<TextBlock Text="{Binding Content}" TextWrapping="Wrap" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And then you can data bind to this from your CS page. ( The above code is just a sample)

How dynamically create PanoramaItem control in c#?

**<controls:PanoramaItem Header="first item">
<!--Double line list with text wrapping-->
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="150">
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="Name" Text="Name: " TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="5,0,0,0"/>
</StackPanel>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock Text="{Binding LineThree}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock Text="{Binding LineFour}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>**
This is .xmal Part. but I have to do this in c#.Then Please Help me how i am do this.
ListBox listBox = new ListBox();
listBox.Margin = new Thickness(0, 0, -12, 0);
listBox.SetBinding(ListBox.ItemsSourceProperty, new Binding("Items"))
CreateItemTemplate(listBox);
PanoramaItem pi = new PanoramaItem();
pi.Header = "first item";
pi.Content = listBox;
When implementing CreateItemTemplate you have two choices, either create the DataTemplate programmatically or create it in a ResourceDictionary as a resource and use that resource. The latter is by far the easiest and best way to do it.
To do it programatically see How to define a DataTemplate in code?
To use a resource you can something like this
public void CreateItemTemplate(ListBox listBox)
{
object myDataTemplate = FindResource("myDataTemplateResource"); // This only works if the resource is available in the scope of your control. E.g. is defined in MyControl.Resources
listBox.SetResourceReference(ListBox.ItemTemplateProperty, myDataTemplate);
}

How to change datatemplate through code

I have ListBox and DataTemplate
I need Set GroupBox Heigth = 300
How to do it?
<DataTemplate x:Key="data_template">
<GroupBox Header="Категория" Width="300" HorizontalAlignment="Stretch" x:Name="GroupBox">
<DockPanel Tag="{Binding id}">
<Button Click="Button_Click" DockPanel.Dock="Top" >
<Button.Content>
<DockPanel>
<TextBlock Text="{Binding title}" TextWrapping="Wrap" DockPanel.Dock="Top" Padding="5" HorizontalAlignment="Center" Foreground="#FFB51414" />
<l:ScrollViewerEx VerticalScrollBarVisibility="Auto" >
<TextBlock Text="{Binding description}" DockPanel.Dock="Top" TextWrapping="Wrap" Padding="5" IsHitTestVisible="False" />
</l:ScrollViewerEx>
</DockPanel>
</Button.Content>
</Button>
</DockPanel>
</GroupBox>
</DataTemplate>
In case, someone tried to resolve my previous question, I did it like the following:
DataTemplate mycolumnDataTemplate = null;
var dataTemplateStream = new SomeClass().GetType().Assembly.GetManifestResourceStream("Some.Namespace.SomeReosurceName.xaml");
string dataTemplateString = new System.IO.StreamReader(dataTemplateStream).ReadToEnd();
dataTemplateString = dataTemplateString.Replace("[0]", browserColumn.ColumnName);
mycolumnDataTemplate = XamlReader.Load(dataTemplateString) as DataTemplate;
What are you trying to achieve? Do you want the GroupBox Height to be changed at the runtime of your application, when some event occurred or some data has changed? If so, then what you are probably looking for is a data trigger or event trigger, which you simply need to add to your DataTemplate.

Categories

Resources