2012-02-27 6 views
3

との結合、私はサンプルユーザーコントロールを作成している動的ユーザーコントロールの静的シルバーで働いているように動作しないとMVVM

RestrictedBox.xaml

<UserControl.Resources> 
     <Converters:EnumToVisibilityConverter x:Key="enumToVisConverter" /> 
     <Converters:EnumToVisibilityConverterReverse x:Key="enumToVisConverterReverse" /> 
    </UserControl.Resources> 
    <Grid x:Name="LayoutRoot" Background="White" Width="Auto"> 
     <StackPanel Margin="0"> 
      <TextBox Text="{Binding Value}" Visibility="{Binding Type,Converter={StaticResource enumToVisConverter}}" BorderBrush="Green" /> 
      <PasswordBox Password="{Binding Value}" Visibility="{Binding Type,Converter={StaticResource enumToVisConverterReverse}}" BorderBrush="Red" /> 
     </StackPanel> 
    </Grid> 

RestrictedBox.xaml.cs

public partial class RestrictedBox : UserControl 
    { 
     public RestrictedBox() 
     { 
      InitializeComponent(); 
      //If i bind static value and uncomment following line then it is working. 
      //If i bind static value and comment following line then it is not working 
      this.DataContext = this; 
      //Dynamic binding does not work using this code. 
     } 

     public string Value 
     { 
      get { return (string)GetValue(ValueProperty); } 
      set { SetValue(ValueProperty, value); } 
     } 
     public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(RestrictedBox), new PropertyMetadata("", ValueChanged)); 
     private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
     } 
     public Mode Type 
     { 
      get { return (Mode)GetValue(TypeProperty); } 
      set { SetValue(TypeProperty, value); } 
     } 
     public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type", typeof(Mode), typeof(RestrictedBox), new PropertyMetadata(TypeChanged)); 
     private static void TypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
     } 
    } 

LoginViewModel.cs

public class LoginViewModel : INotifyPropertyChanged, IRegionMemberLifetime 
{ 
     private string _UserName = "Imdadhusen Sunasara"; 
     public string UserName 
     { 
      get { return _UserName; } 
      set 
      { 
       _UserName = value; 
       OnPropertyChanged("UserName"); 
      } 
     } 
} 

LoginView.xamlこの次の行が結合では動作しません)

<control:RestrictedBox Value="{Binding UserName}" Type="Text" /> 

これは(をスタティックバインディングと

<control:RestrictedBox Value="Imdadhusen" Type="Text" /> 
を働いています

Th anks、 Imdadhusen

+0

まず第一に、私はあなたがあるバインディング何か理解して多くの問題を持っている参照してください。たとえば、「静的バインディング」のようなものはありません。バインディングは「動的」のみにすることができます。私はMattew McDonaldの著書「Pro Silverlight 4 in C#」を一度もお勧めできません。一度読んでみると、膨大な時間を節約できます。 – chopikadze

答えて

2

実際には動作するはずです。下のコントロールの親コンテナのDataContextがviewmodelの他のプロパティを参照していないことを確認してください。

<control:RestrictedBox Value="Imdadhusen" Type="Text" /> 

例えば、以下のようなもの。

<StackPanel DataContext={Binding CurrentUser}> 

<control:RestrictedBox Value="{Binding UserName}" 

Type="Text" /> 

</StackPanel> 

ます。この助けとなる可能性が....

関連する問題