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

Untitled

By: a guest on May 25th, 2012  |  syntax: None  |  size: 2.35 KB  |  hits: 11  |  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. MVVM Binding to Custom Control in Windows Phone
  2. <UserControl x:Class="PhoneApp1.MyControl"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  7. mc:Ignorable="d"
  8. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  9. FontSize="{StaticResource PhoneFontSizeNormal}"
  10. Foreground="{StaticResource PhoneForegroundBrush}"
  11.          d:DesignHeight="480"
  12.          d:DesignWidth="480"
  13.          DataContext="{Binding RelativeSource={RelativeSource Self}}">
  14.  
  15. <Grid x:Name="LayoutRoot">
  16.     <TextBlock Text="{Binding MyLabel}" />
  17. </Grid>
  18.        
  19. public partial class MyControl : UserControl
  20. {
  21.     public MyControl()
  22.     {
  23.         InitializeComponent();
  24.     }
  25.  
  26.     public static readonly DependencyProperty MyLabelProperty =
  27.         DependencyProperty.Register("MyLabel", typeof (string), typeof (MyControl), new PropertyMetadata(default(string)));
  28.  
  29.     public string MyLabel
  30.     {
  31.         get { return (string) GetValue(MyLabelProperty); }
  32.         set { SetValue(MyLabelProperty, value); }
  33.     }
  34. }
  35.        
  36. public class MyControlViewModel : ViewModelBase
  37. {
  38.     private string name;
  39.  
  40.     public string Name
  41.     {
  42.         get { return name; }
  43.         set { name = value;
  44.         RaisePropertyChanged(() => Name);}
  45.     }
  46. }
  47.        
  48. <Application.Resources>
  49.     <PhoneApp1:MyControlViewModel x:Key="MCVM" />
  50. </Application.Resources>
  51.        
  52. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  53.         <PhoneApp1:MyControl  x:Name="testControl" DataContext="{StaticResource MCVM}" MyLabel="{Binding Path=MyLabel}" />            
  54.     </Grid>
  55.        
  56. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  57.         <PhoneApp1:MyControl  x:Name="testControl" MyLabel="{Binding ElementName=Button, Path=Content}" />
  58.         <Button Click="Button_Click" x:Name="Button" Content="Click" />
  59.     </Grid>
  60.        
  61. <TextBlock Text="{Binding MyLabel}" />
  62.        
  63. <TextBlock Text="{Binding MyLabel,
  64.                  RelativeSource={RelativeSource FindAncestor,
  65.                                                 AncestorType=UserControl}" />
  66.        
  67. <UserControl x:Class="PhoneApp1.MyControl"
  68.              x:Name="root"
  69.              x:SnipAttributesBecauseThisIsAnExample="true">
  70.     <TextBlock Text="{Binding MyLabel, ElementName=root}" />
  71. </UserControl>