2009-06-29 21 views
6

Sau đây là code-behind ràng buộc công trình cho SmartFormView điều khiển người dùng:Làm cách nào để gắn kết Chế độ xem này với ViewModel này?

Xem:

<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> 

Mã-đằng sau:

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

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

Tuy nhiên, tôi muốn để ràng buộc SmartFormView vào SmartFormViewModel trong ViewModel của Chế độ xem cuộc gọi, không được mã hóa cứng trong code-behind. Tuy nhiên, hai phương pháp này không ràng buộc:

<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> 

Trong ViewModel Tôi có "kiểm tra" và "SmartFormViewModel" được định nghĩa như là tài sản ViewModel và điền chúng cả (như hình dưới đây), nhưng mặc dù tài sản Testing liên kết tốt thì các SmartFormView không liên kết với nó 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"; 
} 

cú pháp để ràng buộc một usercontrol trong XAML để một ViewModel cụ thể trong là gì gọi ViewModel của View?

Trả lời

6

Có thể sai, nhưng tôi nghĩ bạn chỉ gặp lỗi trong mã của mình.

SmartFormViewModel SmartFormViewModel = new SmartFormViewModel("manageSettingsMain"); 

nên là:

SmartFormViewModel = new SmartFormViewModel("manageSettingsMain"); 

tức. SmartFormViewModelcủa bạn không bao giờ được đặt. Do đó, ràng buộc bạn có trong chế độ xem gốc không tìm thấy.

Tiếp tục vào đó, một cách tốt hơn để làm điều này chỉ là để dính con bạn VM vào cây thị giác:

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

Và sử dụng một DataTemplate để làm việc giải quyết các quan điểm chứ không phải là "cứng mã hóa "chế độ xem vào, um, chế độ xem gốc.

+0

đúng vậy, cảm ơn vì đã tìm thấy điều đó, rất hay để xem, ok vì vậy tôi cũng đã thử cách thứ hai và hoạt động tốt, và điều đó có ý nghĩa hơn với tôi, vì vậy bạn dường như "định nghĩa các vùng trống" sau đó được lấp đầy bởi ViewModels tự động sẽ tự động bị ràng buộc vào DataTemplates thích hợp của họ khi hiển thị, có ý nghĩa, tốt đẹp, cảm ơn! –

Các vấn đề liên quan