에는 ReactiveList
이 포함되어 있고 ChildViewModels
입니다. 나는 그것을 ListView
에 묶어서 ChildViews
을 표시하고 싶습니다. ChildView
는 Label
에 텍스트를 결합하고,이 상태 열거를 기반으로 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
. 내 DataTemplate
에 ViewModel="{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.cs는 ChildViewModel
바인드 및 설정 이미지 데이터 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);
}
}
을, 그러나 그들은 결코하지. ChildViewModels
을 ParentVieWModel.Children
에 추가하면 ChildView
이 생성되지만 올바르게 바인딩되지 않습니다. Children
의 CollectionChanged
이벤트를 구독하고 바인딩을 수동으로 설정할 수는 있지만 적절한 XAML/ReactiveUI 방법을 사용하고 싶습니다. 내가 뭘 놓치고 있니?
위대한 작품입니다! 귀하의 의견을 보내 주셔서 감사합니다. –