2009-06-29 12 views
6

Il seguente code-behind opere vincolante per il controllo utente SmartFormView:Come posso associare questa vista a questo ViewModel?

Vista:

<UserControl x:Class="CodeGenerator.Views.PageItemManageSettingsView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:v="clr-namespace:CodeGenerator.Views" 
    xmlns:vm="clr-namespace:CodeGenerator.ViewModels" 
    Background="#ddd"> 

    <Grid Margin="10"> 
     <ScrollViewer DockPanel.Dock="Top"> 
      <StackPanel Margin="10"> 
       <v:SmartFormView/> 
      </StackPanel> 
     </ScrollViewer> 
    </Grid> 

</UserControl> 

Codice-behind:

using System.Windows.Controls; 
using CodeGenerator.ViewModels; 

namespace CodeGenerator.Views 
{ 
    public partial class SmartFormView : UserControl 
    { 
     public SmartFormView() 
     { 
      InitializeComponent(); 
      DataContext = new SmartFormViewModel("testing"); 
     } 
    } 
} 

Tuttavia, voglio di impegnare la SmartFormView al suo SmartFormViewModel nel ViewModel della vista di chiamata, non codificato in il code-behind. Eppure questi due approcci non legare:

<UserControl.Resources> 
<DataTemplate DataType="{x:Type vm:SmartFormViewModel}"> 
    <v:SmartFormView/> 
</DataTemplate> 
</UserControl.Resources> 

... 

<Grid Margin="10"> 
<ScrollViewer DockPanel.Dock="Top"> 
    <StackPanel Margin="10"> 
     <TextBlock Text="{Binding Testing}"/> 
     <v:SmartFormView DataContext="{Binding SmartFormViewModel}"/> 
     <ContentControl Content="{Binding SmartFormViewModel}"/> 
    </StackPanel> 
</ScrollViewer> 
</Grid> 

Nel ViewModel ho "Testing" e "SmartFormViewModel" definita come proprietà ViewModel e entrambi riempimento (come illustrato di seguito), ma anche se la proprietà Testing si lega bene, il il SmartFormView non si lega al suo SmartFormViewModel:

private SmartFormViewModel _smartFormViewModel=; 
public SmartFormViewModel SmartFormViewModel 
{ 
    get 
    { 
     return _smartFormViewModel; 
    } 
    set 
    { 
     _smartFormViewModel = value; 
     OnPropertyChanged("SmartFormViewModel"); 
    } 
} 

private string _testing; 
public string Testing 
{ 
    get 
    { 
     return _testing; 
    }  
    set 
    { 
     _testing = value; 
     OnPropertyChanged("Testing"); 
    } 
} 

public PageItemManageSettingsViewModel(MainViewModel mainViewModel, PageItem pageItem) 
    : base(mainViewModel, pageItem) 
{ 
    SmartFormViewModel SmartFormViewModel = new SmartFormViewModel("manageSettingsMain"); 
    Testing = "test ok"; 
} 

Qual è la sintassi per associare un UserControl in XAML per un ViewModel specifico nel chiama View ViewModel?

risposta

6

Potrebbe essere sbagliato, ma penso che abbiate solo un bug nel codice.

SmartFormViewModel SmartFormViewModel = new SmartFormViewModel("manageSettingsMain"); 

dovrebbe essere:

SmartFormViewModel = new SmartFormViewModel("manageSettingsMain"); 

es. Il tuo SmartFormViewModelnon viene mai impostato. Pertanto, il legame che hai nella tua vista genitore non lo trova.

Oltre a questo, un modo migliore per farlo è solo per attaccare il bambino VM nella struttura ad albero visuale:

<ContentControl Content="{Binding SmartFormViewModel}"/> 

E utilizzare un DataTemplate per fare la risoluzione della vista, piuttosto che "hard-coding "la vista in, um, vista genitore.

+0

Sì, è stato così, grazie per averlo trovato, bello vedere, ok così ho anche provato il tuo secondo modo e anche quello funziona bene, e questo ha più senso per me, quindi sembri "definire regioni vuote" che vengono quindi riempiti dinamicamente da ViewModels che vengono automaticamente associati ai relativi DataTemplates al momento del rendering, ha senso, bello, grazie! –

Problemi correlati