2012-03-29 3 views
0

私の大きなプロジェクトのバインド変更がうまくいかない理由がわかりません。私はまだ動作しないサンプルプロジェクトに単純化しました。可能な場合は、現在の方法と同じようにdatacontextを設定していきたいと思います。なぜなら、それは他のプロジェクトがそれをやっているからです。次のコードで、SomeTextのテキストがテキストボックスに表示されません。これをどうやって解決するのですか?基本的なSilverlightバインディング

背後にあるコード:

public partial class MainPage : UserControl 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     DataContext = new ViewModel(); 
    } 
} 

データクラス:

public class ViewModel 
{ 
    public string SomeText = "This is some text."; 
} 

メインユーザーコントロール:

<UserControl xmlns:ig="http://schemas.infragistics.com/xaml"   x:Class="XamGridVisibilityBindingTest.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:XamGridVisibilityBindingTest="clr-namespace:XamGridVisibilityBindingTest" mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <TextBox Text="{Binding SomeText}" BorderBrush="#FFE80F0F" Width="100" Height="50"> </TextBox> 
    </Grid> 
</UserControl> 

編集:私は一方向のみ結合を行うにしようとしています。あなたがプロパティを使用して、あなたのVMがINotifyPropertyChangedの継承とSomeTextを変更するたびにPropertyChangedイベントを上げるようにする必要があり

答えて

2

public class ViewModel : INotifyPropertyChanged 
{ 
    private string someText; 

    public event PropertyChangedEventHandler PropertyChanged; 

    public string SomeText 
    { 
     get { return someText; } 
     set 
     { 
      someText = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("SomeText")); 
      } 
     } 
    } 

    public ViewModel() 
    { 
     SomeText = "This is some text."; 
    } 
} 
+0

これは機能しますが、片方向のバインディングが必要です。 –

0

は、私はあなただけのプロパティにバインドすることができ、それを考え出しました!

public class ViewModel 
{ 
    public string SomeText { get; set; } 

    public ViewModel() 
    { 
     SomeText = "This is some text."; 
    } 
}