I'm having trouble getting the Source of my Image set in the code-behind. Here's my XAML:
<StackPanel Name="stkPanel" Height="1200" Width="478" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.723,0.509">
<Image Loaded="imgPicture_Loaded_1" x:Name="imgPicture" ImageOpened="ImgSelectedPicture_ImageOpened_1" Stretch="UniformToFill" Height="309" Width="413" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,30,0,0"></Image>
</StackPanel>
And the code-behind:
private void imgPicture_Loaded_1(object sender, RoutedEventArgs e)
{
imgPict = (sender as Image);
//ScrollViewer scroll = this.LayoutRoot.Children[2] as ScrollViewer;
imgPict.Source = new BitmapImage(new Uri("/project;component/Images/avatar.png", UriKind.RelativeOrAbsolute));
//bindPicture(imgPict);
}
Can anyone see what I'm doing wrong?
First, what i don't understand is the image path "/project;component/Images/avatar.png" don't think the ";" sign makes the path valid. This should work for you:
<StackPanel Name="stkPanel" Height="1200" Width="478" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.723,0.509">
<Image Loaded="imgPicture_Loaded" x:Name="imgPicture" Stretch="UniformToFill"
Height="309" Width="413" HorizontalAlignment="Center" VerticalAlignment="Top"
Margin="0,30,0,0"></Image>
</StackPanel>
then in the code behind
private void imgPicture_Loaded(object sender, RoutedEventArgs e)
{
imgPicture.Source = new BitmapImage(new
Uri("/Images/StoreLogo.png",UriKind.Relative));
}
You can set the image's "Copy to Output Directory" property to "Copy always".
Related
I am displaying list of songs on listbox i have bind media element but i am Unable to get media element name instance in songs.cs file and unable to play song
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<MediaElement Name="Player" Source="{Binding SongUrl}" AutoPlay="False"/>
<Button Name="Click" Click="Play_Click" Content="Button"/>
<StackPanel Width="150" Height="50">
<TextBlock Text="{Binding SongName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" TextAlignment="Center" Foreground="Red" FontSize="16"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
example in Songs.cs:
private void Play_Click(object sender, RoutedEventArgs e)
{
Player.play(); (unable to get Media Element name)
}
The media player should not be inside the ItemTemplate since you will be playing only one sound at a time I believe (it would be costly to have a media element for eaach item). So you should move the MEdiaaPlayer outside and on button click do:
private void Play_Click(object sender, RoutedEventArgs e)
{
Button button=sender as Button;
Player.Source=((Item)button.DataContext).SongUrl
Player.play(); (unable to get Media Element name)
}
For my listbox in windows phone I added a checkbox for each item with the help of this tutorial (option 2). Before I already had a SelectionChanged event for my listbox. How do I prevent firing the event, when the user just checks the checkbox? The SelectionChanged event should only fire when he hits the textbox in the listbox, but not the checkbox.
Thats my listbox:
<ListBox x:Name="toDoItemsListBox" ItemsSource="{Binding Source={StaticResource cvs}}" Grid.Row="1" Margin="12, 0, 12, 0" Width="440" SelectionChanged="goToNavigation" IsSynchronizedWithCurrentItem="False">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<CheckBox IsChecked="{Binding IsFavorite}" Height="48" Width="48" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Style="{StaticResource CheckBoxStyle1}"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Shortcut}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="{Binding BuildingName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
These are my check events:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
ListBoxItem checedItem = this.toDoItemsListBox.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
if (checedItem != null)
{
checedItem.IsSelected = true;
}
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
ListBoxItem checedItem = this.toDoItemsListBox.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
if (checedItem != null)
{
checedItem.IsSelected = false;
}
}
Also I have to mention that the selectionChanged event raises only when I check the checkbox. Not when I uncheck it.
I made it work without the selection changed event. As alternative I used a tap event:
<StackPanel Orientation="Vertical" Tap="StackPanel_Tap">
<TextBlock Text="{Binding Shortcut}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="{Binding BuildingName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
and in Code behind I get my listitem with:
private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
// Save current POI.
Object object = (sender as StackPanel).DataContext as Object;
...}
I have i little and simple problem. I think.
I have to Listpickers where the second depends on the selection from the first.
I've thought that i could easily be done with use of selectionchanged on the first Listpicker and then get the selected index.
<toolkit:ListPicker ExpansionMode="FullScreenOnly" Grid.Row="0" Name="customers" FullModeHeader="Kunder" Margin="10,50,10,10" Width="350" HorizontalAlignment="Left" SelectionChanged="customers_SelectionChanged">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" Margin="12,0,0,0"></TextBlock>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="16,20,0,20">
<TextBlock Text="{Binding Title}" Margin="16,0,0,0" FontSize="30" FontFamily="{StaticResource PhoneFontFamilyLight}"></TextBlock>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
And the code for selectionchanged:
private void customers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selindex = customers.SelectedIndex;
MessageBox.Show("index : " + selindex);
Guid costumerid = customers[selindex].id;
Loadprojects();
}
My problem is that selindex always equals -1 and then i get a out of range exception.
What is the best way to solve this?
private void customers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selindex = customers.SelectedIndex;
if (selindex==-1) return;
MessageBox.Show("index : " + selindex);
Guid costumerid = customers[selindex].id;
Loadprojects();
}
i placed one button in a page .when click on that need to show 1 to 30 numbers in combobox as a popup in that page only.please tell me how to acheive this?
Edit:
I have edited the answer with design,add an image as a local content in the project
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Button" Height="82" HorizontalAlignment="Left" Margin="44,59,0,0" Name="button1" VerticalAlignment="Top" Width="376" Click="button1_Click" />
<ListBox ItemsSource="{Binding item}" Width="376" Name="lst" Margin="56,128,48,76" Background="White">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" DataContext="{Binding}" BorderBrush="Black">
<StackPanel Width="376" Orientation="Vertical" Height="Auto">
<Image Margin="200,20,-75,5" Height="50" Width="50" Source="{Binding img}"></Image>
<TextBlock Margin="-200,-15,90,3" Height="50" Width="50" Name="text" Text="{Binding text}" Foreground="Black"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
lst.visibility = visibility.collapsed;
private void button1_Click(object sender, RoutedEventArgs e)
{
lst.visibility = visibility.visible;
List<Itemss> data = new List<Itemss>();
for (int i = 0; i < 30; i++)
{
Itemss item = new Itemss();
item.text = i.ToString();
item.img = "/images.jpg";
data.Add(item);
}
lst.ItemsSource = data;
}
public class Itemss
{
public string text { get; set; }
public string img { get; set; }
}
}
YOu can make use of the ListPicker for WP7 instead of a ComboBox for WP7.
And to show the ListPicker in a popup, Place the ListPicker in a MessagePrompt.
while programming for windows phone 7, I created a listbox and using DataTemplate which contains a textblock and a textbox. The textbox is hided by default.
The XAML:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Width="460" Height="60" Background="{StaticResource PhoneAccentBrush}">
<TextBlock Text="{Binding data}" FontSize="30" Margin="10,10,10,0"/>
<TextBox Height="60" Width="460" Visibility="Collapsed"/>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The effect I wanna accomplish is : Tap textblock to hide textblock while show textbox.
CODE BEHIND:
private void TextBlock_Tap(object sender, GestureEventArgs e)
{
TextBlock.Visibilty = Visibility.Collapsed;
TextBox.Visibilty = Visibility.Visible;
}
However, obviously the selector isn't correct. I tried to add Name for textbox and textblock, but Name seems does not work in Data Template. Is there anyone who can tell me how can i select the textblock and textbox in a Data Template please? Many Thanks!!!
Try this :
<ListBox Name="lst" >
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Width="460" Height="60" Background="{StaticResource PhoneAccentBrush}" Tap="Canvas_Tap">
<TextBlock Text="{Binding}" FontSize="30" Margin="10,10,10,10"/>
<TextBox Height="60" Width="460" Visibility="Collapsed" Text="text"/>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And in code behind:
private void Canvas_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var m = (sender as Canvas).Children;
foreach (UIElement x in m)
{
if ((x as TextBlock) != null)
(x as TextBlock).Visibility = Visibility.Collapsed;
if ((x as TextBox) != null)
(x as TextBox).Visibility = Visibility.Visible;
}
}
Setting the Name on the template does work, but you can't access it directly!
Instead, try it like this:
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Width="460" Height="60" Background="{StaticResource PhoneAccentBrush}">
<TextBlock x:Name="MyTextBlock" Text="{Binding data}" FontSize="30" Margin="10,10,10,0" Tap="MyTextBlock_Tap" />
<TextBox x:Name="MyTextBox" Height="60" Width="460" Visibility="Collapsed"/>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And the code:
private void MyTextBlock_Tap(object sender, GestureEventArgs e)
{
var elem = (FrameworkElement)sender;
var myTextBlock = (TextBlock)elem.FindName("MyTextBlock");
var myTextBox = (TextBox)elem.FindName("MyTextBox");
myTextBlock.Visibility = Visibility.Collapsed;
myTextBox.Visibility = Visibility.Visible;
}