我有一个包含两个组合框和一个按钮的用户控件(ItemPairing)。我知道我可以重写 ToString() 方法,但我想做的是创建一个返回数组或字典的自定义方法。组合框项目在生成控件时提供,因此这些项目作为参数传递给初始化。应用程序将添加多个 ItemPairing 控件(用户可以添加和删除 ItemPairing 控件以满足他们的需要),然后在用户单击“运行”按钮时迭代所有 ItemPairing 控件。
如果有更简单的方法可以做到这一点,请告诉我。总体思路是让用户动态构建配对项目,然后在点击运行时将每一对组合成一个字典。
项目配对 XAML:
<UserControl x:Class="TableComparisons.ItemPairing"
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"
x:Name="ItemPairingName"
mc:Ignorable="d"
d:DesignHeight="24" d:DesignWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="24"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="24"/>
</Grid.RowDefinitions>
<ComboBox Grid.Row="0" Grid.Column="0" x:Name="Table1_Fields" SelectionChanged="Table1_Fields_SelectionChanged">
</ComboBox>
<ComboBox Grid.Row="0" Grid.Column="1" x:Name="Table2_Fields" SelectionChanged="Table2_Fields_SelectionChanged">
</ComboBox>
<Button Grid.Row="0" Grid.Column="2" Content="X" Click="Button_Click" />
</Grid>
ItempPairing C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TableComparisons
{
public partial class ItemPairing : UserControl
{
public string selectExpr = "None";
public string fields1 = "";
public string fields2 = "";
public override string ToString()
{
return selectExpr;
}
private void updateSE()
{
selectExpr = this.fields1 + " = " + this.fields2;
}
public ItemPairing()
{
InitializeComponent();
List<string> listFields_1 = new List<string>();
listFields_1.Add("One");
listFields_1.Add("Two");
listFields_1.Add("Three");
listFields_1.Add("Four");
Table1_Fields.ItemsSource = listFields_1;
List<string> table2Fields = new List<string>();
table2Fields.Add("A");
table2Fields.Add("B");
table2Fields.Add("C");
table2Fields.Add("D");
Table2_Fields.ItemsSource = table2Fields;
}
public ItemPairing(List<string> table1Fields, List<string> table2Fields)
{
InitializeComponent();
Table1_Fields.ItemsSource = table1Fields;
Table2_Fields.ItemsSource = table2Fields;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
((Panel)this.Parent).Children.Remove(this);
}
private void Table1_Fields_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
fields1 = this.Table1_Fields.SelectedValue.ToString();
updateSE();
}
private void Table2_Fields_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
fields2 = this.Table2_Fields.SelectedValue.ToString();
updateSE();
}
}
}
添加子项配对:
UIElement pi2 = new ItemPairing(table1Fields, table2Fields);
ParingItems.Children.Add(pi2);
遍历 ItemPairing 控件:
private void runPairing_Click(object sender, RoutedEventArgs e)
{
int oCount = 1;
foreach (object o in ParingItems.Children)
{
if (o.GetType() == typeof(ItemPairing))
{
MessageBox.Show(o.ToString());
oCount++;
}
}
}