使用Property+使用构造+构建自定义类+使用List<自定义类>(项目)
帮忙Steve写有关数据库的程序,需要一个AdvancedSearch的功能,使用xaml和C#实现ComboBox的数据录入
AdvancedSearch.xaml:
1 <Window 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="PfgDbTestHarness.AdvancedSearch" 5 Title="Advanced Search" d:DesignHeight="499" d:DesignWidth="753.6" SizeToContent="WidthAndHeight"> 6 <Grid x:Name="MainGrid" ShowGridLines="False"> 7 <Grid.RowDefinitions> 8 <RowDefinition Height="Auto"/> 9 <RowDefinition Height="Auto"/> 10 <RowDefinition Height="Auto"/> 11 </Grid.RowDefinitions> 12 <Grid Margin="5,2"> 13 <Grid.ColumnDefinitions> 14 <ColumnDefinition Width="Auto"/> 15 <ColumnDefinition Width="Auto"/> 16 </Grid.ColumnDefinitions> 17 <TextBlock x:Name="VersionLabel" TextWrapping="Wrap" Text="AdvancedSearchVersion" VerticalAlignment="Center" Margin="4,0"/> 18 <TextBox x:Name="VersionValue" TextWrapping="Wrap" Text="ver.0.0.01" HorizontalAlignment="Center" d:LayoutOverrides="Height" Grid.Column="1" VerticalAlignment="Center" Margin="4,0"/> 19 20 21 </Grid> 22 <StackPanel Margin="5,2" Grid.Row="1"> 23 <Grid x:Name="AdvancedSearchTitle" Margin="0,2"> 24 <Grid.ColumnDefinitions> 25 <ColumnDefinition Width="0.5*"/> 26 <ColumnDefinition Width="0.25*"/> 27 <ColumnDefinition Width="0.25*"/> 28 </Grid.ColumnDefinitions> 29 <TextBlock TextWrapping="Wrap" Text="AdvancedSearchFields" d:LayoutOverrides="Height" HorizontalAlignment="Center" VerticalAlignment="Center"/> 30 <TextBlock TextWrapping="Wrap" Text="MinValue" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1"/> 31 <TextBlock TextWrapping="Wrap" Text="MaxValue" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2"/> 32 </Grid> 33 <Grid x:Name="AdvancedSearchTab1" Margin="0,2"> 34 <Grid.ColumnDefinitions> 35 <ColumnDefinition Width="0.5*"/> 36 <ColumnDefinition Width="0.25*"/> 37 <ColumnDefinition Width="0.25*"/> 38 </Grid.ColumnDefinitions> 39 <ComboBox x:Name="comboBoxSearchOption1" d:LayoutOverrides="Height" Margin="10,4"/> 40 <TextBox x:Name="textBoxSearchOption1_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 41 <TextBox x:Name="textBoxSearchOption1_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 42 </Grid> 43 <Grid x:Name="AdvancedSearchTab2" Margin="0,2"> 44 <Grid.ColumnDefinitions> 45 <ColumnDefinition Width="0.5*"/> 46 <ColumnDefinition Width="0.25*"/> 47 <ColumnDefinition Width="0.25*"/> 48 </Grid.ColumnDefinitions> 49 <ComboBox x:Name="comboBoxSearchOption2" d:LayoutOverrides="Height" Margin="10,4"/> 50 <TextBox x:Name="textBoxSearchOption2_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 51 <TextBox x:Name="textBoxSearchOption2_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 52 </Grid> 53 <Grid x:Name="AdvancedSearchTab3" Margin="0,2"> 54 <Grid.ColumnDefinitions> 55 <ColumnDefinition Width="0.5*"/> 56 <ColumnDefinition Width="0.25*"/> 57 <ColumnDefinition Width="0.25*"/> 58 </Grid.ColumnDefinitions> 59 <ComboBox x:Name="comboBoxSearchOption3" d:LayoutOverrides="Height" Margin="10,4"/> 60 <TextBox x:Name="textBoxSearchOption3_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 61 <TextBox x:Name="textBoxSearchOption3_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 62 </Grid> 63 <Grid x:Name="AdvancedSearchTab4" Margin="0,2"> 64 <Grid.ColumnDefinitions> 65 <ColumnDefinition Width="0.5*"/> 66 <ColumnDefinition Width="0.25*"/> 67 <ColumnDefinition Width="0.25*"/> 68 </Grid.ColumnDefinitions> 69 <ComboBox x:Name="comboBoxSearchOption4" d:LayoutOverrides="Height" Margin="10,4"/> 70 <TextBox x:Name="textBoxSearchOption4_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 71 <TextBox x:Name="textBoxSearchOption4_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 72 </Grid> 73 <Grid x:Name="AdvancedSearchTab5" Margin="0,2"> 74 <Grid.ColumnDefinitions> 75 <ColumnDefinition Width="0.5*"/> 76 <ColumnDefinition Width="0.25*"/> 77 <ColumnDefinition Width="0.25*"/> 78 </Grid.ColumnDefinitions> 79 <ComboBox x:Name="comboBoxSearchOption5" d:LayoutOverrides="Height" Margin="10,4"/> 80 <TextBox x:Name="textBoxSearchOption5_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 81 <TextBox x:Name="textBoxSearchOption5_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 82 </Grid> 83 <Grid x:Name="AdvancedSearchTab6" Margin="0,2"> 84 <Grid.ColumnDefinitions> 85 <ColumnDefinition Width="0.5*"/> 86 <ColumnDefinition Width="0.25*"/> 87 <ColumnDefinition Width="0.25*"/> 88 </Grid.ColumnDefinitions> 89 <ComboBox x:Name="comboBoxSearchOption6" d:LayoutOverrides="Height" Margin="10,4"/> 90 <TextBox x:Name="textBoxSearchOption6_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 91 <TextBox x:Name="textBoxSearchOption6_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 92 </Grid> 93 <Grid x:Name="AdvancedSearchTab7" Margin="0,2"> 94 <Grid.ColumnDefinitions> 95 <ColumnDefinition Width="0.5*"/> 96 <ColumnDefinition Width="0.25*"/> 97 <ColumnDefinition Width="0.25*"/> 98 </Grid.ColumnDefinitions> 99 <ComboBox x:Name="comboBoxSearchOption7" d:LayoutOverrides="Height" Margin="10,4"/> 100 <TextBox x:Name="textBoxSearchOption7_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 101 <TextBox x:Name="textBoxSearchOption7_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 102 </Grid> 103 <Grid x:Name="AdvancedSearchTab8" Margin="0,2"> 104 <Grid.ColumnDefinitions> 105 <ColumnDefinition Width="0.5*"/> 106 <ColumnDefinition Width="0.25*"/> 107 <ColumnDefinition Width="0.25*"/> 108 </Grid.ColumnDefinitions> 109 <ComboBox x:Name="comboBoxSearchOption8" d:LayoutOverrides="Height" Margin="10,4"/> 110 <TextBox x:Name="textBoxSearchOption8_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 111 <TextBox x:Name="textBoxSearchOption8_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 112 </Grid> 113 <Grid x:Name="AdvancedSearchTab9" Margin="0,2"> 114 <Grid.ColumnDefinitions> 115 <ColumnDefinition Width="0.5*"/> 116 <ColumnDefinition Width="0.25*"/> 117 <ColumnDefinition Width="0.25*"/> 118 </Grid.ColumnDefinitions> 119 <ComboBox x:Name="comboBoxSearchOption9" d:LayoutOverrides="Height" Margin="10,4"/> 120 <TextBox x:Name="textBoxSearchOption9_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 121 <TextBox x:Name="textBoxSearchOption9_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 122 </Grid> 123 <Grid x:Name="AdvancedSearchTab10" Margin="0,2,0,2 "> 124 <Grid.ColumnDefinitions> 125 <ColumnDefinition Width="0.5*"/> 126 <ColumnDefinition Width="0.25*"/> 127 <ColumnDefinition Width="0.25*"/> 128 </Grid.ColumnDefinitions> 129 <ComboBox x:Name="comboBoxSearchOption10" d:LayoutOverrides="Height" Margin="10,4"/> 130 <TextBox x:Name="textBoxSearchOption10_MinValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="1"/> 131 <TextBox x:Name="textBoxSearchOption10_MaxValue" Margin="10,4" TextWrapping="Wrap" Text="TextBox" d:LayoutOverrides="Height" Grid.Column="2"/> 132 </Grid> 133 </StackPanel> 134 <Button x:Name="ResetButton" Background="Bisque" Content="Reset" Width="100" Grid.Row="2" Margin="0,14,161,0" HorizontalAlignment="Right" VerticalAlignment="Top" Height="45" FontSize="21.333"/> 135 <Button x:Name="SearchButton" Background="Bisque" Content="Search" Width="100" Grid.Row="2" Height="45" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,14,20,0" FontSize="21.333"/> 136 137 138 </Grid> 139 </Window>
AdvancedSearch.cs:
1 using System; //如下using程序自动生成 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Shapes; 13 14 namespace PfgDbTestHarness 15 { 16 /// <summary> 17 /// Interaction logic for AdvancedSearch.xaml 18 /// </summary> 19 public partial class AdvancedSearch : Window 20 { 21 List<FieldType> _fieldTypes = new List<FieldType>();
22 public AdvancedSearch() //无参数的public构造函数,别的C#文件进入如下函数的入口(构造函数无返回值) 23 { 24 InitializeComponent(); 25 _fieldTypes.Add(new FieldType("Job File Version Number", "VersionNumber", "int")); //构造函数构建List<FieldType> 26 _fieldTypes.Add(new FieldType("Version Comment", "VersionComment","string")); 27 _fieldTypes.Add(new FieldType("Previous 28 ...... //继续
29 PopulateComboBoxes(); 30 } 31 32 private void PopulateComboBoxes() //外部不用调用这个函数private 33 { 34 PopulateComboBox(comboBoxSearchOption1); 35 PopulateComboBox(comboBoxSearchOption2); 36 PopulateComboBox(comboBoxSearchOption3); 37 PopulateComboBox(comboBoxSearchOption4); 38 PopulateComboBox(comboBoxSearchOption5); 39 PopulateComboBox(comboBoxSearchOption6); 40 PopulateComboBox(comboBoxSearchOption7); 41 PopulateComboBox(comboBoxSearchOption8); 42 PopulateComboBox(comboBoxSearchOption9); 43 PopulateComboBox(comboBoxSearchOption10); 44 } 45 46 private void PopulateComboBox (ComboBox comboBox) //外部不用调用这个函数private 47 { 48 comboBox.Items.Clear(); 49 foreach (var fieldType in _fieldTypes) 50 comboBox.Items.Add(fieldType.ReadableName); 51 } 52 53 public class FieldType 54 { 55 public string ReadableName { get; set; } //属性是public的 56 public string InternalName { get; set; } 57 public string DateType { get; set; }
58 public override string ToString() 59 { 60 return ReadableName; 61 }
62 public FieldType(string readableName, string internalName, string dateType) //有参数的构造函数,参数给属性赋值 63 { 64 ReadableName = readableName; 65 InternalName = internalName; 66 DateType = dateType; 67 } 68 } 69 } 70 }