[英]How to preserve a tab in TabControl when using binding
So I have a TabControl
. 所以我有一个
TabControl
。 I want to bind the ItemsSource
to my ViewModel collection so the tabs get added dynamically. 我想将
ItemsSource
绑定到我的ViewModel集合,以便动态添加选项卡。 Something like: 就像是:
<TabControl ItemsSource="{Binding Path=Cities}">
<TabItem Header="Settings" x:Name="settingsTab" />
</TabControl>
Where Cities
is an ObservableCollection
of type City
. 其中
Cities
是类型City
的ObservableCollection
。 However, I want the first tab to display SettingsView
. 但是,我希望第一个选项卡显示
SettingsView
。 The items from Cities
should display CityView
. 来自
Cities
的项目应显示CityView
。 The tab control is just a navigation panel and I'm basically trying to put settings
on the first position, followed by a dynamic number of cities. 标签控件只是一个导航面板,我基本上是在尝试将
settings
放在第一个位置,然后是动态的城市数量。
What's the most elegant solution to this? 最优雅的解决方案是什么? I would prefer to not pollute my view model with some random collection that contains the settings model followed by all the cities models.
我希望不要使用包含设置模型和所有城市模型的随机集合来污染我的视图模型。
If you don't want your tab control to be a list of cities, you need to use DataContext instead of ItemsSource. 如果您不希望选项卡控件成为城市列表,则需要使用DataContext而不是ItemsSource。 Then you can bind the cities object to a list view in the TabItems Content
然后,您可以将城市对象绑定到TabItems内容中的列表视图
example: 例:
public class City
{
public string Name {get;set;}
public string SettingOne {get;set;}
public string SettingTwo {get;set;}
}
<TabControl DataContext="{Binding Path=Cities}">
<TabItem Header="Settings" x:Name="settingsTab">
<TabItem.Content>
<ListView ItemsSource="{Binding}">
<ListView.ItemTemplate>
<TextBox Text="{Binding Path=SettingOne}"></TextBox>
...
</ListView.ItemTemplate>
</StackPanel>
</TabItem.Content>
</TabItem>
</TabControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.