2011-05-04 168 views
1

我一直在玩,並四處尋找如何將一個模型視圖綁定到視圖,但我似乎無法解決它。 我有一個名爲Search的視圖,我想將它綁定到SearchModelView。 查看有一個按鈕和一個文本框,並期待:WPF視圖模型視圖綁定需要幫助請

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 

    <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,40,0,0" Name="comboBox1" VerticalAlignment="Top" Width="174" /> 
    <Label Content="Client:" Height="28" HorizontalAlignment="Left" Margin="0,12,0,0" Name="label1" VerticalAlignment="Top" Width="71" /> 
    <Label Content="Client Reference:" Height="28" HorizontalAlignment="Left" Margin="0,69,0,0" Name="label2" VerticalAlignment="Top" Width="117" /> 
    <TextBox 
     x:Name="clientRefTxt" 
     Text="{Binding Path=ClientRef, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" 
     Height="23" 
     HorizontalAlignment="Left" 
     Margin="12,103,0,0" 
     VerticalAlignment="Top" 
     Width="174" /> 
    <Button 
     Content="Search Debtors" 
     Height="23" 
     HorizontalAlignment="Left" 
     Margin="12,140,0,0" 
     Name="button1" 
     VerticalAlignment="Top" 
     Width="89" 
     Command="{Binding Path=SearchCommand}"/> 

</Grid> 

而且我希望它綁定到SearchViewModel:

命名空間Master.ViewModel {

public class SearchViewModel:WorkspaceViewModel 
{ 
    RelayCommand _searchCommand; 
    readonly Search _search; 


    #region Search Properties 
    public string ClientRef 
    { 

     get { MessageBox.Show("GET CLIENTREF"); return _search.ClientRef; } 
     set 
     { 
      MessageBox.Show("SET CLIENTREF"); 
      if (value == _search.ClientRef) 
       return; 
      _search.ClientRef = value; 
      base.OnPropertyChanged("ClientRef"); 
     } 
    } 

    #endregion 

    public ICommand SearchCommand 
    { 
     get 
     { 
      MessageBox.Show("SEARCHCOMMAND"); 

      if (_searchCommand == null) 
      { 
       _searchCommand = new RelayCommand(
        param=> this.Search(), 
        param=> this.CanSearch 
        ); 
      } 
      return _searchCommand; 
     } 
    } 

    public void Search() 
    { 
     MessageBox.Show("SEARCHING"); 
    } 

    bool CanSearch 
    { 
     get { return true; } 
    } 
} 

}

我刪除了頂部的所有程序集,但假設它們都在那裏。另外請注意,SearchViewModel是在一個單獨的DLL中,而不是在與視圖的EXE。 任何幫助將是偉大的或至少是一個指針的寫入方向,我已經閱讀了MSDN上的MVVM文章,並沒有幫助...我需要一個更好的破壞綁定這些片斷。 在此先感謝。 P.S. 更多的細節: SearchViewModel屬於Master.ViewModel 搜索查看是和GUI.View 的一部分,我有想法綁定的對象是如何工作的,我不是來確保對如何看法綁定到視圖模型

回答

1

您是否查看網格?我只使用UserControl或Window類型作爲視圖,但使用網格可能會取得成功。

無論如何,這是使用UserControl視圖實例化ViewModel的最簡單方法。如果您使用網格,只需用網格標籤替換UserControl標籤即可。

<UserControl ...(blah blah) 
    xmlns:viewmodel="clr-namespace:Master.ViewModel"> 
    <UserControl.DataContext> 
     <viewmodel:SearchViewModel/> 
    </UserControl.DataContext> 

我相信,保持了查看代碼的必要,除非是MVVM的首選模式 - 讓XAML線東西你可能的情況下。

1

您需要將視圖的DataContext設置爲視圖模型的實例。有各種各樣的這樣做的方法,包括那絲它自動的框架,但上手最簡單的方法是做它在視圖的構造函數:

partial class Search : Window 
{ 
    public Search() 
    { 
    InitializeComponent();    // provided by Visual Studio 

    DataContext = new SearchViewModel(); // all-important! 
    } 
} 

顯然,你可能需要提供其他信息來初始化SearchViewModel,但希望這足以讓您走上正確的軌道。

0

您將需要像@itowlson建議的那樣引導您的應用程序。

但是,如果你有多個ViewModel,你應該允許WPF爲你做。這樣做的基本方法(在開始有十幾個視圖之前很容易維護)是創建一個DataTemplate,以將View與ModelView(大多數人稱爲ViewModel)聯繫起來。

所以,你所提供的XAML可能是一個用戶控件(至少應該是),所以你需要做幾件事情

首先創建一個資源字典
(快速方法是右鍵單擊您的項目,然後點擊Add -> Resource Dictionary

在該文件中(讓我們將其命名爲Resources.xaml)把這個:

<DataTemplate DataType="{x:Type vm:SearchViewModel}"> 
    <vw:SearchView> 
</DataTemplate> 

以上是假設你把命名空間vwvm用於查看和視圖模型分別命名空間

轉到您的App.xaml,並把這個:

<Application.Resources> 
    <ResourceDictionary Source="Resources.xaml"/> 
</Application.Resources> 

上面會告訴WPF,每當遇到SearchViewModel類型的對象:

  • 實例化一個SearchView對象
  • 將它的DataContext設置爲SearchViewModel對象

HTH