简体   繁体   English

如何以编程方式将图像添加到 WPF 选项卡控件项

[英]How to Programatically Add Images to WPF Tab Control Item

I am creating TabItems programmatically without any issue by using the following code:我使用以下代码以编程方式创建 TabItems,没有任何问题:

var tabItem = new TabItem();
tabItem.Header = "My Tab Header";
tabItem.Content = new UserControl1();
MainTabControl.Items.Add(tabItem); 

Now when a tab item added to tab control i also want to add image button at the same time with the creation of TabItem aligned at right side.现在,当一个选项卡项添加到选项卡控件时,我还想同时添加图像按钮,同时创建 TabItem 对齐在右侧。 How can i achieve this?我怎样才能做到这一点? thanks in advance.提前致谢。

EDIT: I have tried a lot and still did not get an idea.编辑:我已经尝试了很多,但仍然没有得到一个想法。 following is my tabcontrol in xaml and ObservableCollection.以下是我在 xaml 和 ObservableCollection 中的 tabcontrol。 When i run project tabs shows successfully but i don't know how to add images in it because in my tab control in xaml, it does not have TabItems markup and they are displaying automatically when running project.当我运行项目选项卡显示成功但我不知道如何在其中添加图像时,因为在我的 xaml 选项卡控件中,它没有 TabItems 标记并且它们在运行项目时自动显示。 Please view my sample code and transform into my desired result.请查看我的示例代码并转换为我想要的结果。 I wana conclude and close this issue, I Really thanks and appreciate help.我想结束并关闭这个问题,我真的很感谢并感谢帮助。

Following is xaml:以下是xaml:

  <TabControl ItemsSource="{Binding TabItems, Mode=TwoWay}" DisplayMemberPath="Content" HorizontalAlignment="Left" Height="73" Margin="10,25,0,0" VerticalAlignment="Top" Width="312"/>

Following is viewmodel以下是视图模型

     namespace WpfApplication1.ViewModels
    {
    public class VMTabControl : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyname));
            }
        [![enter image description here][1]][1]}

        public VMTabControl()
        {
            TabItems = new ObservableCollection<clsTabs>(GetList().ToList());
        }

        private ObservableCollection<clsTabs> _tabItems;
        public ObservableCollection<clsTabs> TabItems
        {
            get { return _tabItems; }
            set
            {
                _tabItems = value;
                OnPropertyChanged("TabItems");
            }
        }

        public List<clsTabs> GetList()
        {
            List<clsTabs> tablist = new List<clsTabs>();
            tablist.Add(new clsTabs { Content = "First", ImgPath = "path" });
            tablist.Add(new clsTabs { Content = "Second", ImgPath = "path" });
            return tablist;
        }

    }
}

In code在代码中

TabItem.Header is not limited to displaying strings - you can set any UI control on it. TabItem.Header不限于显示字符串 - 您可以在其上设置任何 UI 控件。 For example:例如:

tabItem.Header = new Button { Content = "Click me" };

To display both text and a close button, you could use a horizontal stack panel that contains a text block and a button.要同时显示文本和关闭按钮,您可以使用包含文本块和按钮的水平堆栈面板。

In XAML在 XAML 中

However, UI layouts are most often written in XAML.但是,UI 布局通常是用 XAML 编写的。 The following XAML assumes you have an Items property in your view model, which is a collection of items.以下 XAML 假设您的视图模型中有一个Items属性,它是一个项目的集合。 These items have a TabHeaderName and TabImagePath property.这些项目具有TabHeaderNameTabImagePath属性。 The view model should also have a RemoveTabCommand property, which is an ICommand that takes a single argument (the tab item to be removed):视图模型还应该有一个RemoveTabCommand属性,它是一个带有单个参数(要删除的选项卡项)的ICommand

<TabControl ItemsSource="{Binding Items}">
    <!-- // If you only need to display a single property, you can use DisplayMemberPath.
         // If you need something more fancy (such as a text-block and button next to each other),
         // you'll have to provide a tab header template instead: -->
    <TabControl.ItemTemplate>
        <DataTemplate>
            <!-- // Tab item header template (this is how each tab header will look): -->
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding TabHeaderName}" />
                <Button Content="X"
                        Command="{Binding DataContext.RemoveTabCommand, RelativeSource={RelativeSource AncestorType=TabControl}}"
                        CommandParameter="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <!-- // Tab item content template (this is how each tab content will look): -->
            <Image Source="{Binding TabImagePath}" />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

If Items is an observable collection, simply adding an item to it will automatically add a tab item for it.如果Items是一个 observable 集合,只需向其中添加一个项目就会自动为其添加一个选项卡项目。 Likewise, removing an item will remove its tab item.同样,删除项目将删除其选项卡项目。

Try this:尝试这个:

    var tabItem = new TabItem();
    var stack = new StackPanel();
    var t = new TextBlock();
    t.Text = "My Tab Header";
    var i = new Image();
    //i.Source = ...
    stack.Children.Add(t);
    stack.Children.Add(i);
    tabItem.Header = stack;
    tabItem.Content = new StackPanel();
    tab.Items.Add(tabItem);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM