亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何創建WPF用戶控件:自定義TabItem?

如何創建WPF用戶控件:自定義TabItem?

C#
弒天下 2023-07-22 15:50:19
預先感謝您的支持!我正在嘗試創建一個自定義選項卡項來充當動態創建 UI 元素的畫布。這是一張圖片,讓您了解我想要在此自定義控件中實現的功能:我需要能夠在父窗體中動態生成選項卡項到 TabControl - 問題是我的代碼似乎對 TabItem 沒有任何作用 - 它總是空白,并且不會抱怨我的代碼。少了什么東西?謝謝你的幫助!我的 WPF 用戶控件 tabitem 代碼:<UserControl x:Class="Configuration_Manager.SearchTab"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              xmlns:local="clr-namespace:Configuration_Manager"             mc:Ignorable="d"              d:DesignHeight="450" d:DesignWidth="800">    <TabItem Header="Search Configuration Name">        <StackPanel>            <Grid>                <Grid.ColumnDefinitions>                    <ColumnDefinition Width="*" />                </Grid.ColumnDefinitions>                <GroupBox Header="Git Repo Credentials:">                    <StackPanel>                        <Grid>                            <Grid.ColumnDefinitions>                                <ColumnDefinition Width="*" />                                <ColumnDefinition Width="3*" />                            </Grid.ColumnDefinitions>                            <Label Grid.Column="0" Content="Server Address:" />                            <TextBox Grid.Column="1" Margin="2" />                        </Grid>                        <Grid>                            <Grid.ColumnDefinitions>                                <ColumnDefinition Width="*" />                                <ColumnDefinition Width="3*" />                            </Grid.ColumnDefinitions>                            <Label Grid.Column="0" Content="Username:" />                            <TextBox Grid.Column="1" Margin="2" />                        </Grid>設計師:
查看完整描述

1 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

在 WPF 中,如果您想動態創建控件,則始終必須使用模板。TabControl是一個ItemsControl. TabItem元素(選項卡)是為ItemsControl.ItemsSource 集合內的每個項目自動生成的。TabItem可以使用樣式和模板來設計其視覺效果。

使用TabControl.ContentTemplate屬性指定DataTemplate每個選項卡的內容


首先,您必須創建應在TabItem.


TabData.cs:


public class TabData : INotifyPropertyChanged

{

  public TabData(string header)

  {

    this.Header = header;

  }


  public string Header { get; set; }


  private string serverAddress;

  public string ServerAddress

  {

    get => this.serverAddress;

    set

    {

      if (value == this.serverAddress) return;

      this.serverAddress = value;

      OnPropertyChanged();

    }

  }


  private string username;

  public string Username

  {

    get => this.username;

    set

    {

      if (value == this.username) return;

      this.username = value;

      OnPropertyChanged();

    }

  }


  private string password;

  public string Password

  {

    get => this.password;

    set

    {

      if (value == this.password) return;

      this.password = value;

      OnPropertyChanged();

    }

  }


  public event PropertyChangedEventHandler PropertyChanged;    

  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)

  {

    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

  }

}

然后創建一個視圖模型來處理選項卡數據并公開TabControl.

視圖模型是DataContext的TabControl。


ViewModel.cs


public class ViewModel: INotifyPropertyChanged

{

  public ViewModel()

  {

    this.TabDatas = new ObservableCollection<TabData>()

    {

      new TabData("First Tab"),

      new TabData("Second Tab"),

      new TabData("Third Tab")

    };

  }


  // Adding a new TabData item to the TabDatas collection 

  // will dynamically create a new TabItem inside the TabControl

  public void AddNewTab()

  {

    this.TabDatas.Add(new TabData("Fourth Tab"));

  }



  public ObservableCollection<TabData> TabDatas { get; set; }


  public event PropertyChangedEventHandler PropertyChanged;    

  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)

  {

    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

  }

}

AddNewTab可以從ICommand(例如單擊按鈕時)或某些事件(例如可用的新數據)調用。


主窗口.xaml:


<Window>

  <Window.DataContext>

    <ViewModel />

  </Window.DataContext>


  <Grid>


    <!-- Use DisplayMemberPath property to set the source property for the tab header -->

    <TabControl x:Name="TabControl" 

                ItemsSource="{Binding TabDatas}" 

                DisplayMemberPath="Header" >


      <!-- Use a DataTemplate to design the visual appearance of the TabItem content -->

      <TabControl.ContentTemplate>

        <DataTemplate DataType="TabData">

          <StackPanel>

            <Grid>

              <Grid.ColumnDefinitions>

                <ColumnDefinition Width="*" />

              </Grid.ColumnDefinitions>

              <GroupBox Header="Git Repo Credentials:">

                <StackPanel>

                  <Grid>

                    <Grid.ColumnDefinitions>

                      <ColumnDefinition Width="*" />

                      <ColumnDefinition Width="3*" />

                    </Grid.ColumnDefinitions>

                    <Label Grid.Column="0"

                           Content="Server Address:" />

                    <TextBox Grid.Column="1"

                             Margin="2" 

                             Text="{Binding ServerAddress}" />

                  </Grid>

                  <Grid>

                    <Grid.ColumnDefinitions>

                      <ColumnDefinition Width="*" />

                      <ColumnDefinition Width="3*" />

                    </Grid.ColumnDefinitions>

                    <Label Grid.Column="0"

                           Content="Username:" />

                    <TextBox Grid.Column="1"

                             Margin="2"

                             Text="{Binding Username}"/>

                  </Grid>

                  <Grid>

                    <Grid.ColumnDefinitions>

                      <ColumnDefinition Width="*" />

                      <ColumnDefinition Width="3*" />

                    </Grid.ColumnDefinitions>

                    <Label Grid.Column="0"

                           Content="Password:" />

                    <TextBox Grid.Column="1"

                             Margin="2"

                             Text="{Binding Password}"/>

                  </Grid>

                  <Grid>

                    <Grid.ColumnDefinitions>

                      <ColumnDefinition Width="*" />

                      <ColumnDefinition Width="2*" />

                      <ColumnDefinition Width="*" />

                    </Grid.ColumnDefinitions>

                    <CheckBox x:Name="CheckBoxStoreCredentials"

                              Content="Store Credentials"

                              Grid.Column="0"

                              IsChecked="False"

                              VerticalAlignment="Center" />

                    <Button x:Name="ButtonDownloadConfiguration"

                            Content="Download Configuration"

                            Grid.Column="2"

                            Margin="5" />

                  </Grid>

                </StackPanel>

              </GroupBox>

            </Grid>

          </StackPanel>

        </DataTemplate>

      </TabControl.ContentTemplate>

    </TabControl>

  </Grid>

</Window>



查看完整回答
反對 回復 2023-07-22
  • 1 回答
  • 0 關注
  • 308 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號