我想在使用MVVM模式的UWP中创建一个应用程序。 有可能是用户控件是Listbox中的项目的DataTemplate将拥有自己的虚拟机。MVVM uwp UserControl与VM作为DataTemplate
这里是MainPage.xaml中的一部分
<ListBox Name="ListBox1" ItemsSource="{Binding Components}">
<ListBox.ItemTemplate >
<DataTemplate x:DataType="vm:ComponentUserControlViewModel" >
<local:ComponentUserControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
MainPageVM包含:
public ObservableCollection<Component> Components
现在这是我的用户
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Text="{Binding Id}" Grid.Row="0"></TextBox>
<TextBox Text="{Binding Name}" Grid.Row="1"></TextBox>
</Grid>
VM:
public class ComponentUserControlViewModel : ViewModelBase
{
private string _componentId;
private string _componentName;
public ComponentUserControlViewModel()
{
}
public string Id
{
get { return _componentId; }
set
{
SetProperty(ref _componentId, value);
}
}
public string Name
{
get { return _componentName; }
set
{
SetProperty(ref _componentName, value);
}
}
我想要的是例如如果我在我的UI中更改Id
属性,则视图模型Id
属性也将更改。
您的ComponentUserControl需要一个(依赖性)属性,以便您可以执行'' –
您可以更具体一些。我添加了这个道具,但我该如何使用它。我不知道它应该如何工作。 –