2016-10-27 5 views
0

에는 ReactiveList이 포함되어 있고 ChildViewModels입니다. 나는 그것을 ListView에 묶어서 ChildViews을 표시하고 싶습니다. ChildViewLabel에 텍스트를 결합하고,이 상태 열거를 기반으로 Image 자원 설정합니다 간단한 컨테이너 ChildViewModels뷰 모델의 ReactiveList를 뷰 모델의 뷰를 포함하는 ListView에 바인딩하는 방법

public class ParentViewModel : ReactiveObject 
{ 
    public ReactiveList<ChildViewModel> Children { get; } 
     = new ReactiveList<ChildViewModel>(); 
} 

ChildViewModel.cs에 대한

ParentViewModel.cs를 텍스트와 Status을 가지고

public class ChildViewModel : ReactiveObject 
{ 
    public string SomeText { get; set; } 

    public enum Status 
    { 
     Unknown, 
     Known 
    } 

    public Status CurrentStatus { get; set; } = Status.Unknown; 
} 

parentview.x 데이터 템플릿에 ChildView을 사용하여 ListView을 래핑하는 amlUserControl. 내 DataTemplateViewModel="{Binding}"을 추가했는지 확인했습니다.

<UserControl x:Class="MyProject.UI.ParentView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:vm="clr-namespace:MyProject.View_Models" 
     xmlns:ui="clr-namespace:MyProject.UI"> 
    <ListView x:Name="ChildList" BorderThickness="0"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
      <!--I have also tried the following line, didn't work --> 
      <!--<DataTemplate DataType="{x:Type vm:ChildViewModel}">--> 
       <ui:ChildView ViewModel="{Binding}"/> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</UserControl> 

ParentView.xaml.cs에게ParentViewModel에 대한 종속성 속성을 작성 및 Image와 함께 뷰 모델의 Children ChildList 목록보기에

public partial class ParentView : UserControl, IViewFor<ParentViewModel> 
{ 
    public static readonly DependencyProperty ViewModelProperty 
     = DependencyProperty.Register(
      "ViewModel", 
      typeof(ParentViewModel), 
      typeof(ParentView)); 

    object IViewFor.ViewModel 
    { 
     get { return ViewModel; } 
     set { ViewModel = (ParentViewModel)value; } 
    } 

    public ParentViewModel ViewModel 
    { 
     get { return (ParentViewModel)GetValue(ViewModelProperty); } 
     set 
     { 
      SetValue(ViewModelProperty, value); 
      BindToViewModel(value); 
     } 
    } 

    private void BindToViewModel(ParentViewModel viewModel) 
    { 
     this.OneWayBind(viewModel, vm => vm.Children, v => v.ChildList.ItemsSource); 
    } 
} 

ChildView.xaml 간단한 UserControl을 결합 a Label

<UserControl x:Class="MyProject.UI.ChildView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel Orientation="Horizontal"> 
     <Image Name="StatusIcon" Margin="2" /> 
     <Label Name="DisplayName" /> 
    </StackPanel> 
</UserControl> 

ChildView.xaml.csChildViewModel 바인드 및 설정 이미지 데이터 I는 뷰 모델 속성 공격 있는지 ChildView에 중단 점을 설정 한

public partial class ChildView : UserControl, IViewFor<ChildViewModel> 
{ 
    public static readonly DependencyProperty ViewModelProperty 
     = DependencyProperty.Register(
      "ViewModel", 
      typeof(ChildViewModel), 
      typeof(ChildView)); 

    public ChildView() 
    { 
     InitializeComponent(); 
    } 

    object IViewFor.ViewModel 
    { 
     get { return ViewModel; } 
     set { ViewModel = (ChildViewModel)value; } 
    } 

    public ChildViewModel ViewModel 
    { 
     get { return (ChildViewModel)GetValue(ViewModelProperty); } 
     set 
     { 
      SetValue(ViewModelProperty, value); 
      BindToViewModel(value); 
     } 
    } 

    private void BindToViewModel(ChildViewModel viewModel) 
    { 
     viewModel 
      .WhenAnyValue(vm => vm.CurrentStatus) 
      .Subscribe(status => 
      { 
       // set StatusIcon's image based on status 
      }); 

     this.Bind(viewModel, vm => vm.DisplayName, v => v.SomeText); 
    } 
} 

을, 그러나 그들은 결코하지. ChildViewModelsParentVieWModel.Children에 추가하면 ChildView이 생성되지만 올바르게 바인딩되지 않습니다. ChildrenCollectionChanged 이벤트를 구독하고 바인딩을 수동으로 설정할 수는 있지만 적절한 XAML/ReactiveUI 방법을 사용하고 싶습니다. 내가 뭘 놓치고 있니?

답변

0

ChildView가 등록 할 수있는 ViewModel에서 변경 사항을 발생 시키도록 ChildViewModel에서 속성을 완전히 설정해야한다고 생각합니다. ViewModels documentation을 참조하십시오.

string someText ; 
public string SomeText { 
    get { return someText; } 
    set { this.RaiseAndSetIfChanged(ref someText , value); } 
} 

Status currentStatus; 
public Status CurrentStatus { 
    get { return currentStatus; } 
    set { this.RaiseAndSetIfChanged(ref currentStatus, value); } 
} 

또는 둘 중 하나만 읽으면됩니다.

readonly ObservableAsPropertyHelper<string> someText; 
public string SomeText{ 
    get { return someText.Value; } 
} 

readonly ObservableAsPropertyHelper<Status> currentStatus; 
public Status CurrentStatus{ 
    get { return currentStatus.Value; } 
} 
+0

위대한 작품입니다! 귀하의 의견을 보내 주셔서 감사합니다. –

관련 문제