How i get the control inside dataTemplate in windows phone? - windows

I want change the color os background of the border each listboxitem increase.
<ListBox.ItemTemplate>
<DataTemplate>
<border x:name: border>
<ListBoxItem ItemSource={Binding Example}>
</ListBoxItem>
</border>
Any Ideas?

First check this link, on how to use converters.
Then in your XAML, write your border like this
<Border BorderBrush="{Binding Converter=ColorConverter}">
....
<Border>
Modify your converter code to something like this
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//Define some random colors
Color[] colors = { Colors.Blue, Colors.Brown, Colors.Cyan, Colors.Green, Colors.Magenta, Colors.Orange, Colors.Purple, Colors.Yellow, Colors.LightGray };
return colors[(new Random()).Next(8)];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
}
}
So, this code dynamically returns one of the colors. And there is the chance for getting same color continuously. Btw, I didn't test the above code.

The TypeConverter for "IValueConverter" does not support converting
when i put the border like above causes this error.
here is my complete code
<ItemsControl x:Name="listaAdd" ItemsSource="{Binding sample}" Grid.Row="0" Margin="0,221,0,0" Foreground="White" Background="#FF5B5B5B" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" Grid.Row="1" Width="480" >
<Border x:Name="borda" BorderBrush="{Binding Converter=ColorConverter}" >
<ListBoxItem x:Name="listSelected" Foreground="White" IsSelected="True" VerticalAlignment="Center" FontSize="{StaticResource PhoneFontSizeLarge}" Content="{Binding Nome}" Background="{x:Null}" HorizontalContentAlignment="Left" Height="80" DoubleTap="listSelected_DoubleTap">
</ListBoxItem>
</Border>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="subMenulist">
<Button Grid.Column="1" Content="delete" BorderThickness="0" Margin="0" Background="White" Foreground="#FF1A739D" FontSize="32" HorizontalContentAlignment="Left">
</Button>
<Button Grid.Column="1" Content="compartilhar" Margin="0,0,0,0" x:Name="btnShare" BorderThickness="0" Background="White" Foreground="#FF1A739D" FontSize="32" HorizontalContentAlignment="Left">
</Button>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Related

How to set external style for text block based on the language set by the user in windows phone 8

My windows phone 8 application should work in two languages English and Arabic.
In this application i used text block control in many places i.e. in List Boxes and also as individual for Page Headers.
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" VerticalAlignment="Center">
<TextBlock x:Name="title" Text="{Binding LocalizedResources.CategoriesText, Source={StaticResource LocalizedStrings}}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" FontWeight="Bold" FontSize="40" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Fonts/nazli.ttf#nazli"/>
</StackPanel>
Now in List Box also i have text block.
<Grid x:Name="ContentPanel" Grid.Row="2" Background="White">
<ListBox x:Name="categoriesList"
SelectionChanged="categoriesList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="categoryGrid" Height="60" Margin="0,0,0,0" MinWidth="480" Background="{Binding Converter={StaticResource RowColour}}" >
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<!--<RowDefinition Height="10"/>-->
</Grid.RowDefinitions>
<StackPanel x:Name="dataRow" Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock x:Name="category" Text="{Binding CategoryName}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" MinWidth="420"/>
<Image x:Name="arrow" Stretch="Fill" Margin="0,0,0,0" Source="{Binding ArrowImageName}" Height="20" HorizontalAlignment="Right"/>
</StackPanel>
<!--<Image x:Name="line" Grid.Row="1" Width="460" HorizontalAlignment="Center" Source="Images/separator.png" />-->
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
By default the application is in English language. So I want to use the default Font Family Segoe WP for all Text Blocks when the application is in English.
When the user changes the Application's Language from English to Arabic, then I want to use the embedded Font nazli.ttf for all Text Blocks in the application.
So for that I embedded one external font in my application and set the content type to Copy Always. The font is nazli.ttf.
Blow is the external style which works for English. But I need an External resource which should work for both English and Arabic Languages.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style x:Key="NormalTextStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="30"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FontFamily" Value="Segoe WP"/>
<Setter Property="Foreground" Value="Yellow"/>
</Style>
</ResourceDictionary>
So how should be the external resource file to satisfy the above requirement.
One way would be to use a converter on the TextBlocks:
<TextBlock FontFamily="{Binding Converter={StaticResource FontConverter}}">some text</TextBlock>
converter:
public class FontConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (culture.Name.StartsWith("ar"))
{
return new FontFamily("/MyApp;Component/Fonts/Fonts.zip#fontName");
}
else
{
return new FontFamily("Segoe WP");
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Adjust the paths above as appropriate.
And the converter declared somewhere as:
<converters:FontConverter x:Key="FontConverter" />

How to Reference the ViewModel in the View

I am attempting to reference a class in my ViewModel within the xaml of my view, and I am getting an error saying Object reference not set to an instance of an object. THe error occurs when attemping to set the ViewModel as the Resource for a ListBox. Also, when attempting to set the ItemsSource property of my ListBox, another error results stating The resource "effects" could not be resolved.
MainPage.xaml
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Resources>
//Error occurs here!
<vm:EffectItems x:Key="effects"/>
</Grid.Resources>
//The ItemsSource property thus contains an error as well
<ListBox Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{StaticResource effects}" SelectionChanged="ListBox_SelectionChanged"
toolkit:TiltEffect.IsTiltEnabled="True"
ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" ItemWidth="152" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="14,0,0,10" >
<Image Source="{Binding Thumbnail}" Width="128" Height="128" />
<TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I have also tried the following setup which results in the same errors on the same items
<ListBox Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{StaticResource effects}" SelectionChanged="ListBox_SelectionChanged"
toolkit:TiltEffect.IsTiltEnabled="True"
ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Auto">
<ListBox.Resources>
<vm:EffectItems x:Key="effects"/>
</ListBox.Resources>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" ItemWidth="152" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="14,0,0,10" >
<Image Source="{Binding Thumbnail}" Width="128" Height="128" />
<TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ViewModel class
public class EffectItems : ObservableCollection<EffectItem>
{
public EffectItems()
{
Add(new EffectItem(new BlackWhiteEffect(), "data/icons/BlackWhite.png"));
Add(new EffectItem(new SepiaEffect(), "data/icons/Sepia.png"));
Add(new EffectItem(new TiltShiftEffect { UpperFallOff = 0.2f, LowerFallOff = 1.0f }, "data/icons/TiltShift.png"));
Add(new EffectItem(new PolaroidEffect { Tinting = 0.8f }, "data/icons/PolaYellow.png", "Pola"));
}
}
At the top of my page I have xmlns:vm="clr-namespace:AppName.ViewModels" which contains no errors.
You can bind your ViewModel to a View by setting the views DataContext. The straight forward way is to set it in the constructor of the code behind:
// Constructor
public MainPage()
{
InitializeComponent();
DataContext = new EffectItems();
}
Then you can set the ItemsSource of your List to the DataContext by using the default binding:
ItemsSource="{Binding}"

Windows 8 - XAML - Conditional background for row in ListView

I have a data source with items in it. Each item has a boolean called "IsAvailable".
I want to render the datasource in a ListView. The background of each row must be green if "IsAvailable" = true or red if "IsAvailable" = false.
How can I do it?
Thanks
-- Marco
This is the code:
<ListView x:Name="frItemlistView" Margin="10,40,666,10" SelectionMode="None" ItemsSource="{Binding Source={StaticResource availableItemsViewSource}}" IsSwipeEnabled="false" IsItemClickEnabled="True" ItemClick="frItemlistView_ItemClick_1" Grid.Row="1" DoubleTapped="frItemlistView_DoubleTapped_1" RightTapped="frItemlistView_RightTapped_1" BorderThickness="35,0,35,35" Grid.RowSpan="2">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="110" Margin="6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
<TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/>
<TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/>
<TextBlock Text="{Binding Volume}" Style="{StaticResource BodyTextStyle}" MaxHeight="60"/>
<TextBlock Text="{Binding IsAvailable}" Style="{StaticResource BodyTextStyle}" MaxHeight="60" />
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
You can use converter to do conditional formatting of DataTemplate. You need to bind IsAvailable property to background of StackPanel, converter will give appropriate color.
Add a class with this definition.
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI;
public class AvailabilityToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var Availability = (bool)value;
var color = Availability ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red);
return color;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
XAML
local is the XAML namespace reference of AvailabilityToColorConverter class, which is in <Page />
<Page.Resources>
<local:AvailabilityToColorConverter x:Key="AvailabilityToColor" />
</Page.Resources>
<ListView x:Name="frItemlistView" Margin="10,40,666,10" SelectionMode="None" ItemsSource="{Binding Source={StaticResource availableItemsViewSource}}" IsSwipeEnabled="false" IsItemClickEnabled="True" ItemClick="frItemlistView_ItemClick_1" Grid.Row="1" DoubleTapped="frItemlistView_DoubleTapped_1" RightTapped="frItemlistView_RightTapped_1" BorderThickness="35,0,35,35" Grid.RowSpan="2">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="110" Margin="6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0" Background="{Binding IsAvailable, Converter={StaticResource AvailabilityToColor}}">
<TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/>
<TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/>
<TextBlock Text="{Binding Volume}" Style="{StaticResource BodyTextStyle}" MaxHeight="60"/>
<TextBlock Text="{Binding IsAvailable}" Style="{StaticResource BodyTextStyle}" MaxHeight="60" />
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

How to use Listbox in wp7?

I have one listbox control in my xaml par.My code for listbox is
<ScrollViewer Grid.Row="2" Name="ScrollGrid" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" Height="Auto" Width="450" Margin="0,100,0,0" >
<ListBox x:Name="TransactionList" Grid.Row="2" HorizontalAlignment="Center" Margin="0,0,0,0" Width="400" Height="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0,-10,0,0" Height="120" Width="400" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="80" Width="300" Margin="0,0,20,0">
<TextBlock Height="Auto" Margin="20,0,0,0" VerticalAlignment="Center" Text="vodafone vas" FontSize="30" Foreground="Gray" Name="tbCitynameFavorite" />
</StackPanel>
<StackPanel Orientation="Vertical" Height="60" Width="60" Margin="0,0,0,30">
<Image Name="imgfevdlt" Width="50" Height="40" VerticalAlignment="Center" FlowDirection="LeftToRight" Source="/VodafoneAugmentedReality;component/Images/ArrowMoreSmall.png" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="10" Width="350" Margin="-360,60,0,0">
<Image Source="/VodafoneAugmentedReality;component/Images/SaperaterLine.png" Width="350" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
When i run application this will not display any data
But when i comment out this two tag than it will run perfectly
<ListBox.ItemTemplate>
<DataTemplate>
Here i have only static value so it will ok but when i have multiple value than it will create problem
So plase help me i can solve this problem?
You will have to set up a class (say ListBoxItem ) which contains all the properties of your DataTemplate members values
And then define a List of ListBoxItem 's and set it as the ItemSource of your ListBox
Example code
public class ListBoxItem
{
public string CityNameFavorite { set; get; }
// Other required properties follows here
}
List<ListBoxItem> listBoxItems = new List<ListBoxItem>();
listBoxItems.Add(new ListBoxItem() { CityNameFavorite = "Vodafone vas" });
//Add as many items you need
TransactionList.ItemsSource = listBoxItems;
This is just an overview of how it can be achieved, improvise based on your needs

Image not visible in RSS feed

I am developing a Windows phone app where the rss link is given in the app. The news result is displayed properly but the images are not being displayed. This is how i displayed the image in xaml
<ListBox Name="feedListBox" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" Height="528" HorizontalAlignment="Left" Margin="9,97,0,0" VerticalAlignment="Top" Width="439" SelectionChanged="feedListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<Image Name="img" Source="{Binding ImageUri}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0" />
<StackPanel VerticalAlignment="Top">
<TextBlock TextDecorations="Underline" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" />
<TextBlock Name="feedSummary" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" />
<TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate.DateTime}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and in .cs this is how i retrieve
img = feed.ImageUrl;
feedListBox.ItemsSource = feed.Items;
how do i get the images in my app?
Thanx
You can't directly do img = image as it's a template for each list item, not a certain image on a screen. Try to use a converter to transform Url from your feed into an Uri object:
public class UrlToUriConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return new Uri(value.toString(), UriKind.Absolute);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
Edit: More info:
Create an UrlToUriConverter converter as above.
Insert that converter into your page resources:
<phone:PhoneApplicationPage.Resources>
<src:UrlToUriConverter x:Key="UrlToUri"/> </phone:PhoneApplicationPage.Resources>
src is a namespace of the converter and it should be added in <phone:PhoneApplicationPage tag, eg. xmlns:src="clr-namespace:TestProject"
Use that converter in Image.Source binding:
<Image Source="{Binding LineOne, Converter={StaticResource UrlToUri}}" >

Resources