WPF create UserControl for a TabItem
本文关键字:TabItem for UserControl create WPF | 更新日期: 2023-09-27 18:27:05
我需要帮助用用户控件替换一些xaml代码。我在主窗口中有两个选项卡,我想把第二个选项卡(显示为Header=WCF)放在用户控件中。这是我的代码:
<Window x:Class="TesterTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:helpers="clr-namespace:TesterTool"
xmlns:da ="clr-namespace:TesterTool.DatabaseAccess"
xmlns:converters="clr-namespace:TesterTool.Converters"
xmlns:vm="clr-namespace:TesterTool"
Title="TestingTool v1.00" Height="350" Width="725"
Icon="Icon.ico">
<Window.Resources>
<converters:BoolVisibilityConverter x:Key="BoolVisibilityConverter" />
<converters:InvertedBoolVisibilityConverter x:Key="InvertedBoolVisibilityConverter"/>
</Window.Resources>
<TabControl>
<TabItem Header="WPF" Margin="-2,0" Height="22" VerticalAlignment="Bottom">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="image.png"/>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image gif:ImageBehavior.AnimatedSource="332.gif"
Width="152"
Height="39"
Margin="10,69,194,37"
Grid.Row="1"
Visibility="{Binding IsLoading, Converter={StaticResource BoolVisibilityConverter}}"/>
<TextBox x:Name="tb1"
Text ="{Binding BoxSerialNumber}"
Height="23" Margin="128,26,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
HorizontalAlignment="Left" Width="220"
/>
<TextBox x:Name="tbn2"
Visibility="{Binding IsViewLogSelected, Converter={StaticResource InvertedBoolVisibilityConverter}}"
Height="23" Margin="128,73,8,0"
TextWrapping="Wrap"
Text="{Binding HardwareID}"
VerticalAlignment="Top"
RenderTransformOrigin="0.494,0.478"/>
<TextBox x:Name="tbb3"
HorizontalAlignment="Left"
Height="23"
Margin="128,116,0,0"
TextWrapping="Wrap"
Text="{Binding CompanyContext}"
VerticalAlignment="Top" Width="220"/>
<Label Content="Box Serial Number" HorizontalAlignment="Left" Margin="0,26,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,0.5"/>
<Label Content="Hardware ID" HorizontalAlignment="Left" Margin="0,73,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,0.5"/>
<Label Content="Company Context" HorizontalAlignment="Left" Margin="3,113,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,0.5"/>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
Visibility="{Binding IsTrafficLogSelected, Converter={StaticResource BoolVisibilityConverter}}">
<Label Content="Start Date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp1"
SelectedDate="{Binding StartDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25"/>
<Label Content="End Date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp2"
SelectedDate="{Binding EndDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25"/>
</StackPanel>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
Visibility="{Binding IsTransmissionLogSelected, Converter={StaticResource BoolVisibilityConverter}}">
<Label Content="Start Date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp13"
SelectedDate="{Binding StartDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25"/>
<Label Content="End Date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp23"
SelectedDate="{Binding EndDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25"/>
</StackPanel>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
Visibility="{Binding IsCreditLogSelected, Converter={StaticResource BoolVisibilityConverter}}">
<Label Content="Log date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp12"
SelectedDate="{Binding LogDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25" Width="102"/>
</StackPanel>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
Visibility="{Binding IsViewLogSelected, Converter={StaticResource BoolVisibilityConverter}}">
</StackPanel>
<Label Content="VesselIMO" HorizontalAlignment="Left" Margin="32,70,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,0.5" Grid.Column="1"/>
<Label Content="Select Type" Grid.Column="1" HorizontalAlignment="Left" Margin="32,22,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name ="cb"
ItemsSource="{Binding LogTypes}"
SelectedItem="{Binding SelectedItem}"
Margin="164,26,23,0" VerticalAlignment="Top"
Grid.Column="1">
</ComboBox>
<TextBox x:Name="tb3_Copy"
Visibility="{Binding IsViewLogSelected, Converter={StaticResource InvertedBoolVisibilityConverter}}"
HorizontalAlignment="Left"
Height="23"
Margin="164,73,0,0"
TextWrapping="Wrap"
Text="{Binding VesselIMO}"
VerticalAlignment="Top"
Width="172" Grid.Column="1"/>
<Button Name ="Button1"
Content="Submit"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="197,81,0,0"
Grid.Row="1"
VerticalAlignment="Top"
Width="152"
Height="39"
Click="Button1Clicked"
IsEnabled="{Binding IsLoading, Converter={StaticResource InvertedBoolVisibilityConverter}}"/>
<TextBox Grid.Column="1"
HorizontalAlignment="Left"
Height="23"
Margin="164,120,0,0"
TextWrapping="Wrap"
Text ="{Binding Counter}"
VerticalAlignment="Top"
Width="91"/>
<Label Content="Number of rows" Grid.Column="1" HorizontalAlignment="Left" Margin="32,116,0,0" VerticalAlignment="Top"/>
</Grid>
</TabItem>
<TabItem Header="WCF">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="User Credentials" HorizontalAlignment="Left" Margin="10,20,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="26" Width="96"/>
<Label Content="Username" HorizontalAlignment="Left" Margin="10,69,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="26" Width="63"/>
<Label Content="Password" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="26" Width="60"/>
<TextBox x:Name="unTB"
Text="{Binding Username}"
HorizontalAlignment="Left"
Height="22"
Margin="103,73,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="179"
Grid.Column="1"/>
<Label Content="Box ID"
HorizontalAlignment="Left"
Margin="384,73,0,0"
VerticalAlignment="Top"
Grid.Column="1"
Height="26"
Width="44"/>
<Label Content="Software Version" HorizontalAlignment="Left" Margin="384,124,0,0" VerticalAlignment="Top" Grid.Column="1" Height="26" Width="99"/>
<TextBox x:Name="boxTB"
Text="{Binding BoxID}"
HorizontalAlignment="Left"
Height="22"
Margin="501,73,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="179"
Grid.Column="1"/>
<TextBox
Text ="{Binding SoftwareId}"
x:Name="SIBox"
HorizontalAlignment="Left"
Height="22"
Margin="501,124,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="179"
Grid.Column="1"/>
<Button Content="Create Box" HorizontalAlignment="Left" Margin="538,233,0,0" VerticalAlignment="Top" Width="142" Height="35" Click="Button_Click" Grid.Column="1" Command="{Binding CreateBoxCommand}"/>
<TextBox x:Name="pass"
Text ="{Binding Password}"
HorizontalAlignment="Left"
Height="22"
Margin="103,120,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="179"
Grid.Column="1"/>
</Grid>
</TabItem>
</TabControl>
有人知道如何以正确的方式做到这一点吗?
-
将整个网格及其内容放在用户控件中的TabItem(WCF)中,并将此UserControl命名为WCFTabUserControl。
-
引用Window标签中的名称空间,如:
xmlns:uc="clr-namespace:WpfApplication1"
-
使用您的用户控件:
<TabItem Header="WCF"> <uc:WCFTabUserControl/> </TabItem>