2009-10-18 40 views
0

我有一個真正簡單的例子,我有一個項目 類實現INotifyPropertyChanged是很好的工作沒有一個視圖模型:Silverlight的MVVM視圖模型查看約束力不工作

public class Item : INotifyPropertyChanged 
     { 
      private string _name; 
      private string _desc; 
      public string Name 
      { 
       get 
       { return _name; } 
       set 
       { 
        if (_name == value) 
         return; 
        _name = value; 
        this.OnPropertyChanged(new PropertyChangedEventArgs("Name")); 
       } 
      } 
         } 
      public event PropertyChangedEventHandler PropertyChanged; 

      public Item() 
      { 

      } 

      protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) 
      { 
       if (this.PropertyChanged != null) 
        this.PropertyChanged(this, e); 
      } 

     } 

該作品在一個簡單的例子,我有下面的代碼背後:

public MainPage() 
    { 
     InitializeComponent(); 

     item = new Item() { Name = "john", Description = "this is my name" }; 
     LayoutRoot.DataContext = item; 
    } 

,如果我有一個按鈕,在代碼中調用命令處理程序背後我看到改變 到簡單的用戶界面爲的PropertyChanged由於 的Twoway綁定在我的文本屬性上,因此「事件正在」收聽「。處理程序是這樣的:

private void Button_Click(object sender, RoutedEventArgs e) 
    { 


     item.Name = "ted"; 
     item.Description = "adddress"; 


    } 

所以當然這改變了用戶界面(我說這是真正簡單的例子) 現在我介紹一個視圖模型,並更改到簡單的模型沒有反映 沒有一個傾聽財產變化。

我設置的視圖中的視圖模型DataContext的,事實上這是代碼視圖中唯一的一行:

LayoutRoot.DataContext = new MyViewModel(); 

現在我的視圖模型的樣子:

public class MyViewModel 
    { 
     Item item = null; 
     public ICommand ChangeCommand { get; private set; } 
     public MyViewModel() 
     { 
      item = new Item() { Name = "john", Description = "jjjj" }; 
      ChangeCommand = new DelegateCommand<string>(OnChange); 


     } 
     private void OnChange(string title) 
     { 
      item.Name = "bill"; 

     } 

注: 我使用的模式和實踐命令從XAML發射事件到Viewmodel 和這個偉大工程

我的處理程序再次改變了item.name,導致setter被調用,並且 this.OnPropertyChanged(new PropertyChangedEventArgs(「Name」));被叫,但沒有人是 傾聽,所以沒有改變。

我想保持這個例子真正簡單,有些人會說太簡單,但我只想 知道爲什麼綁定不起作用。我可以從視圖拉ViewModel,但我需要 能夠做到相反。我想知道我錯過了什麼。

+0

我想我正確地猜出了你的問題,但問題會更清楚,如果你另外包含XAML數據綁定代碼。 – 2009-10-19 01:19:54

回答

0

你結合MyViewModel的一個實例,它沒有公開曝光性質。嘗試製作並綁定到公共MyViewModel.Item屬性。

1

實際上ü似乎是在正確的軌道上,我注意到了同樣的事情,改變了代碼的樣子:

private Item _item; 

      public ICommand ChangeCommand { get; private set; } 
      public MyViewModel() 
      { 
       myitem = new Item() { Name = "john", Description = "jjjj" }; 
       ChangeCommand = new DelegateCommand<string>(OnChange); 
      } 
      private void OnChange(string title) 
      { 
       _item.Name = "bill"; 
    //   SelectedItem = _item; 

      } 
      public Item myitem 
      { 
       get{return _item;} 
       set 
       { 
        _item = value; 
        RaisePropertyChanged(new PropertyChangedEventArgs("myitem")); 
       } 
      } 

但我的領域,直到我把格在我的文本框仍然未填寫並設置DataContext的通電網的DataContext =「{結合myitem>在下面的XAML代碼:

- <UserControl 
    x:Class="BindingStart.MainPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:viewmodel="clr-namespace:BindingStart" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

     xmlns:Commands="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation" 
     mc:Ignorable="d" d:DesignWidth="640" 
    d:DesignHeight="480" 
      > 
     <UserControl.Resources> 
      <viewmodel:MyViewModel x:Key="ViewModel" /> 
     </UserControl.Resources> <Grid x:Name="LayoutRoot" 
    DataContext="{StaticResource 
    ViewModel}"> 
      <StackPanel> 
       <Grid DataContext="{Binding myitem}"> 
       <Grid.RowDefinitions> 
       <RowDefinition Height="22" /> 
       <RowDefinition Height="22" /> 
       <RowDefinition Height="22" /> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="300" /> 
       <ColumnDefinition Width="20" /> 
      </Grid.ColumnDefinitions> 

      <TextBlock Grid.Row="0" Grid.Column="0" 
    Foreground="Black">SupplierName</TextBlock> 
      <TextBox x:Name="SName" Grid.Row="0" Grid.Column="1 " 
    IsReadOnly="True"  Text="{Binding 
    Name, Mode=TwoWay}" /> 

現在,我可以在代碼中設置的背景之下,有點像的XAML解決方案不知道我爲什麼我需要做的。這一步,但它似乎工作