我想在使用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需要一個(依賴性)屬性,以便您可以執行'' –
您可以更具體一些。我添加了這個道具,但我該如何使用它。我不知道它應該如何工作。 –