2 回答

TA貢獻(xiàn)1816條經(jīng)驗 獲得超4個贊
是的,我找到了答案。通過編碼,我嘗試了很多,但無法做到這一點,但以我在 XAML 方面所做的簡單方式。只需復(fù)制代碼并將每個用戶控件分別綁定到 DataCollection 變量中來自后端的一個數(shù)據(jù)。無需從后端代碼綁定每個用戶控件。下面是我的 WPF 文件的 XAML 代碼。為每個用戶控件反復(fù)完成以綁定在表單中。
<UniformGrid Rows="1">
<ItemsControl ItemsSource="{Binding DataCollection}" Name="UserControlColumn1" Margin="4,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:UserControlColumn1 HorizontalAlignment="Left" Margin="2" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UniformGrid>
<UniformGrid Rows="1">
<ItemsControl ItemsSource="{Binding DataCollection}" Name="UserControlColumn2" Margin="4,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:UserControlColumn2 HorizontalAlignment="Left" Margin="2" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UniformGrid>
<!--Same for other user Controls as above two...-->

TA貢獻(xiàn)1847條經(jīng)驗 獲得超7個贊
每個用戶控件都有自己的數(shù)據(jù)上下文,因此在主視圖模型中為每個用戶控件視圖模型添加一個屬性,然后綁定它。像這樣的東西:
UserControl1ViewModel
{
....
}
MainViewModel
{
UserControl1ViewModel UserControl1ViewModel {get; private set;}
}
- 2 回答
- 0 關(guān)注
- 341 瀏覽
添加回答
舉報