[英]ContentControl.Content not binding correctly in MVVM
I have an MVVM app. 我有一个MVVM应用程序。 I want a collection of buttons to be represented from the ViewModel and be dynamic. 我希望从ViewModel表示按钮的集合并使其动态化。 Which means I want to populate the window with controls from the ViewModel. 这意味着我想用ViewModel中的控件填充窗口。
I tried creating a content control and binding it's Content property to a Grid which I will put buttons in. The binding did not work, it remains empty. 我尝试创建一个内容控件,并将其Content属性绑定到一个网格中,然后将按钮放入其中。该绑定无效,它保持为空。
I tried binding it to a simple string, still nothing. 我试图将其绑定到一个简单的字符串,仍然没有。 I should mention that other simple bindings do work, so that's why it's weird. 我应该提到其他简单的绑定也可以使用,因此这很奇怪。
The creation of the UserControl: UserControl的创建:
<TabControl HorizontalAlignment="Left" Height="696" Margin="429,0,0,32" VerticalAlignment="Bottom" Width="552" ItemsSource="{Binding TabCollection}">
<TabControl.Resources>
</TabControl.Resources>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<cattab:CategoryTab/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>`
The binding in the UserControl: UserControl中的绑定:
<ContentControl HorizontalAlignment="Left" Height="286" Margin="98,152,0,-396" VerticalAlignment="Top" Width="313">
<ContentControl Content="{Binding Favorites}" Margin="0,30,0,0" VerticalAlignment="Top"/>
</ContentControl>`
MainViewModel: MainViewModel:
//**** Initilize TabCollection with fake data (temporary)
TabCollection.Add(new CategoryTabViewModel { Header = "בדיקה11" });
TabCollection.Add(new CategoryTabViewModel { Header = "בדיקה2" });
UserControl ViewModel: UserControl ViewModel:
public CategoryTabViewModel()
{
SearchText = "bbbaaaa";
Favorites.Add(new Button());
}
The binding of SearchText works, on Favorites it's not SearchText的绑定有效,但在“收藏夹”上无效
Try to use an ItemsControl
尝试使用ItemsControl
Here's a good tutorial: http://www.wpf-tutorial.com/list-controls/itemscontrol/ 这是一个很好的教程: http : //www.wpf-tutorial.com/list-controls/itemscontrol/
<ItemsControl HorizontalAlignment="Left" Height="286" Margin="98,152,0,-396" VerticalAlignment="Top" Width="313" Content="{Binding Favorites}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Use a WrapPanel
to manage your layout: http://www.wpftutorial.net/WrapPanel.html 使用WrapPanel
管理布局: http : //www.wpftutorial.net/WrapPanel.html
After reading your updates I can say that your code is not MVVM compliant because your ViewModel layer is aware of the View layer (you create Buttons in your ViewModel) 阅读您的更新后,我可以说您的代码不符合MVVM,因为您的ViewModel层知道View层(您可以在ViewModel中创建Button)
What you need to do: 你需要做什么:
XAML of your CategoryTab 您的CategoryTab的XAML
<ItemsControl ItemsSource="{Binding Favorites, Mode=OneWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button/><!--Show whatever you want here-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
If you want to create new Button every time an object is added to Favorites you will need to make Favorites an ObservableCollection. 如果要在每次将对象添加到“收藏夹”时创建新的Button,则需要将“收藏夹”设置为ObservableCollection。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.