Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 6.10 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Databinding not working correctly
  2. <phone:PhoneApplicationPage
  3. x:Class="PhoneApp2.PivotPage1"
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  7. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  8. xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
  9. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  10. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  11. xmlns:p="clr-namespace:PhoneApp2"
  12. mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
  13. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  14. FontSize="{StaticResource PhoneFontSizeNormal}"
  15. Foreground="{StaticResource PhoneForegroundBrush}"
  16. SupportedOrientations="Portrait"  Orientation="Portrait"
  17. shell:SystemTray.IsVisible="True">
  18. <phone:PhoneApplicationPage.Resources>
  19.     <p:CollectionToVisibilityConverter x:Key="CollectionToVisibility" />
  20.     <ControlTemplate x:Key="Template">
  21.         <StackPanel>
  22.             <ListBox ItemsSource="{Binding}" Visibility="{Binding Converter={StaticResource CollectionToVisibility}, ConverterParameter=false}">
  23.                 <ListBox.ItemTemplate>
  24.                     <DataTemplate>
  25.                         <StackPanel Orientation="Horizontal">
  26.                             <TextBlock Text="{Binding Text1}" Padding="5,0" />
  27.                             <TextBlock Text="{Binding Text2}" />
  28.                         </StackPanel>
  29.                     </DataTemplate>
  30.                 </ListBox.ItemTemplate>
  31.             </ListBox>
  32.             <TextBlock Text="Empty" Visibility="{Binding Converter={StaticResource CollectionToVisibility}, ConverterParameter=true}" />
  33.         </StackPanel>
  34.  
  35.     </ControlTemplate>
  36. </phone:PhoneApplicationPage.Resources>
  37. <Grid x:Name="LayoutRoot" Background="Transparent">
  38.     <Grid.RowDefinitions>
  39.         <RowDefinition />
  40.         <RowDefinition Height="150" />
  41.     </Grid.RowDefinitions>
  42.     <controls:Pivot Title="MY APPLICATION">
  43.         <controls:PivotItem Header="item1" DataContext="{Binding Model1}" Template="{StaticResource Template}" />
  44.         <controls:PivotItem Header="item2" DataContext="{Binding Model2}" Template="{StaticResource Template}" />
  45.         <controls:PivotItem Header="item3" DataContext="{Binding Model3}" Template="{StaticResource Template}" />
  46.         <controls:PivotItem Header="item4" DataContext="{Binding Model4}" Template="{StaticResource Template}" />
  47.         <controls:PivotItem Header="item5" DataContext="{Binding Model5}" Template="{StaticResource Template}" />
  48.     </controls:Pivot>
  49.     <StackPanel Grid.Row="1">
  50.         <Button Content="Clear Items" Click="Button_Click" />
  51.         <Button Content="Add Items" Click="Button_Click_1" />
  52.     </StackPanel>
  53.  
  54. </Grid>
  55.        
  56. public partial class PivotPage1 : PhoneApplicationPage
  57. {
  58.     public PivotPage1()
  59.     {
  60.         InitializeComponent();
  61.         var vm = new ViewModel();
  62.  
  63.         vm.Model1 = new ObservableCollection<Model>();
  64.         vm.Model2 = new ObservableCollection<Model>();
  65.         vm.Model3 = new ObservableCollection<Model>();
  66.         vm.Model4 = new ObservableCollection<Model>();
  67.         vm.Model5 = new ObservableCollection<Model>();
  68.  
  69.         Populate(vm);
  70.  
  71.         DataContext = vm;
  72.     }
  73.  
  74.     private void Populate(ViewModel vm)
  75.     {
  76.         vm.Model1.Add(new Model { Text1 = "1", Text2 = "World" });
  77.         vm.Model1.Add(new Model { Text1 = "1", Text2 = "Planet" });
  78.  
  79.         vm.Model2.Add(new Model { Text1 = "2", Text2 = "World" });
  80.         vm.Model2.Add(new Model { Text1 = "2", Text2 = "Planet" });
  81.  
  82.         vm.Model3.Add(new Model { Text1 = "3", Text2 = "World" });
  83.         vm.Model3.Add(new Model { Text1 = "3", Text2 = "Planet" });
  84.  
  85.         vm.Model4.Add(new Model { Text1 = "4", Text2 = "World" });
  86.         vm.Model4.Add(new Model { Text1 = "4", Text2 = "Planet" });
  87.  
  88.         vm.Model5.Add(new Model { Text1 = "5", Text2 = "World" });
  89.         vm.Model5.Add(new Model { Text1 = "5", Text2 = "Planet" });
  90.     }
  91.  
  92.     private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
  93.     {
  94.         ViewModel vm = DataContext as ViewModel;
  95.         vm.Model1.Clear();
  96.         vm.Model2.Clear();
  97.         vm.Model3.Clear();
  98.         vm.Model4.Clear();
  99.         vm.Model5.Clear();
  100.     }
  101.  
  102.     private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
  103.     {
  104.         ViewModel vm = DataContext as ViewModel;
  105.         Populate(vm);
  106.     }
  107. }
  108.  
  109. public class ViewModel
  110. {
  111.     public ObservableCollection<Model> Model1 { get; set; }
  112.     public ObservableCollection<Model> Model2 { get; set; }
  113.     public ObservableCollection<Model> Model3 { get; set; }
  114.     public ObservableCollection<Model> Model4 { get; set; }
  115.     public ObservableCollection<Model> Model5 { get; set; }
  116. }
  117.  
  118. public class Model
  119. {
  120.     public string Text1 { get; set; }
  121.     public string Text2 { get; set; }
  122. }
  123.        
  124. public class CollectionToVisibilityConverter : IValueConverter
  125. {
  126.     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  127.     {
  128.         IList list = value as IList;
  129.         bool isEmptyVisible = false;
  130.         if (parameter != null)
  131.             bool.TryParse(parameter.ToString(), out isEmptyVisible);
  132.  
  133.         if (list == null || list.Count == 0)
  134.             return isEmptyVisible ? Visibility.Visible : Visibility.Collapsed;
  135.  
  136.         return isEmptyVisible ? Visibility.Collapsed : Visibility.Visible;
  137.     }
  138.  
  139.     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  140.     {
  141.         throw new NotImplementedException();
  142.     }
  143. }
  144.        
  145. public class Model : INotifyPropertyChanged
  146. {
  147.     private string m_Text1;
  148.     public string Text1
  149.     {
  150.         get { return m_Text1;}
  151.         set { m_Text1 = value;
  152.               OnPropertyChanged(new PropertyChangedEventArgs("Text1")); }
  153.  
  154.     // ...
  155.  
  156.     #region INotifyPropertyChanged: Shared bit
  157.     public event PropertyChangedEventHandler PropertyChanged;
  158.  
  159.     private void OnPropertyChanged(PropertyChangedEventArgs e)
  160.     {
  161.     if (PropertyChanged != null)
  162.         PropertyChanged(this, e);
  163.     }
  164.     #endregion
  165.  
  166. }