码迷,mamicode.com
首页 > Windows程序 > 详细

WPF TabControl only load the selected TabItem

时间:2016-03-08 23:56:36      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:

1.binding ItemsSource

 public class TabItemViewModel
 {
        public string Header { get; set; }
        public FrameworkElement Content { get; set; }
 }
xmlns:sc="clr-namespace:System.Collections;assembly=mscorlib"
<Window.Resources> <sc:ArrayList x:Key="Datas"> <local:TabItemViewModel Header="uer1"> <local:TabItemViewModel.Content> <local:UserControl1 /> </local:TabItemViewModel.Content> </local:TabItemViewModel> <local:TabItemViewModel Header="uer2"> <local:TabItemViewModel.Content> <local:UserControl2 /> </local:TabItemViewModel.Content> </local:TabItemViewModel> </sc:ArrayList> </Window.Resources> <TabControl ItemsSource="{StaticResource Datas}"> <TabControl.ItemContainerStyle> <Style TargetType="TabItem"> <Setter Property="Header" Value="{Binding Header}"/> <Setter Property="Content" Value="{Binding Content}"/> </Style> </TabControl.ItemContainerStyle> </TabControl>

2. use TabItem

<TabControl SelectionChanged="TabControl_SelectionChanged">
    <TabItem Header="zzu Red"">
        <Grid Background="Red"/>
    </TabItem>
    <TabItem Header="zzu Orange">
        <Grid Background="Orange"/>
    </TabItem>
</TabControl>
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    TabControl tabControl = sender as TabControl;
    for (int i = 0; i < tabControl.Items.Count; i++)
    {
        TabItem item = tabControl.Items[i] as TabItem;
        if (item.IsSelected)
        {
            if (item.Content == null)
            {
                item.Content = item.Tag;
            }
            else
            {
                item.Tag = item.Content;
            }
        }
        else
        {
            item.Tag = item.Content;
            item.Content = null;
        }
    }
}

 

WPF TabControl only load the selected TabItem

标签:

原文地址:http://www.cnblogs.com/iekwei/p/5256269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!