2009-06-29 7 views
6

다음은 SmartFormView 사용자 제어를위한 작품을 바인딩 코드 뒤에 다음어떻게이 뷰를이 ViewModel에 바인딩 할 수 있습니까?

보기

:

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

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

그러나, 나는 바인딩 할 : 코드 숨김

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

SmartFormView를 호출 뷰의 ViewModel에서 SmartFormViewModel 으로 하드 코딩하지 않음 코드 숨김. 그러나이 두 가지 방법은 결합하지 않습니다

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

을 뷰 모델에서 테스트 속성이 잘 결합하지만 내가 뷰 모델 속성으로 정의 "테스트"와 "SmartFormViewModel"를 가지고 (아래 그림 참조) 둘 다 작성하지만,,하여 SmartFormViewSmartFormViewModel에 결합하지 않습니다

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"; 
} 

는 특정 뷰 모델에 XAML의 UserControl을 결합 할 수있는 구문 무엇입니까 View의 ViewModel을 호출합니까?

답변

6

틀릴 수도 있지만 코드에 버그가있는 것 같습니다.

SmartFormViewModel SmartFormViewModel = new SmartFormViewModel("manageSettingsMain"); 

은 다음과 같아야합니다

SmartFormViewModel = new SmartFormViewModel("manageSettingsMain"); 

즉. 귀하의 SmartFormViewModel은 결코 설정되지 않습니다.. 따라서 부모보기에서 바인딩을 찾지 못합니다.

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

그리고 하드 코딩 "보다는 뷰의 해상도를 할 수있는 DataTemplate을를 사용 : 이것 또한

이 할 수있는 더 좋은 방법이 시각적 트리에 자녀 VM을 스틱 그냥 "부모보기로보기.

+0

네, 그걸 찾아 주셔서 고마워요, 알았어요, 좋아요. 그래서 저는 두 번째 방법을 시도해 보았습니다. 그리고 그것은 잘 작동합니다. 그리고 그것은 나에게 더 의미가 있습니다. 그래서 여러분은 "빈 영역을 정의하는 것"으로 보입니다. ViewModels에 의해 채워지며, 자동으로 렌더링시 적절한 DataTemplate에 바인딩됩니다. –

관련 문제