我有一个usercontrol(ItemPairing),它包含两个组合框和一个按钮。我知道我可以覆盖ToString()方法,但我想要做的是创建一个返回数组或字典的自定义方法。生成控件时会提供组合框项,以便将这些项作为参数传递给初始化。该应用程序将添加多个ItemPairing控件(用户可以添加和删除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();
}
}
}
添加子ItemPairing:
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++;
}
}
}
答案 0 :(得分:0)
如果你想编写有效的WPF,那么你应该停止尝试使用它,就像它是Windows Forms ... WPF就是数据绑定。您应该查看MSDN上的Data Binding Overview页面以获得深入介绍。
要回答您的问题,UserControl
提供某些数据的一般方法是实现DependencyProperty
:
public static readonly DependencyProperty ResultsProperty = DependencyProperty.
Register("Results", typeof(Dictionary<string, string>),
typeof(ItemPairing), new UIPropertyMetadata(Dictionary<string, string>.Empty));
public Dictionary<string, string> Results
{
get { return (Dictionary<string, string>)GetValue(ResultsProperty); }
set { SetValue(ResultsProperty, value); }
}
您没有提到Dictionary
的类型,所以我只是假设它是Dictionary<string, string>
。
然后您可以Bind
内外UserControl
<YourXmlNamespace:ItemPairing Results="{Binding YourExternalResultsProperty}" />
来此属性:
Results
更新&gt;&gt;&gt;
您可以通过将主视图模型设置为UserControl
的{{1}}来处理从多个DataContext
聚合MainWindow
个集合的情况。该视图模型可以包含多个属性,一个属性可以绑定到每个UserControl
Results
属性,也可以包含您可能正在使用的任何其他视图模型类型的属性。
无论哪种方式,通过使用这样的主视图模型,您应该有一个包含绑定到所有Results
集合的属性的类,无论是否在其他视图模型中,您都可以访问去商场。然后你可以有一个方法从所有其他的值中创建一个新的Dictionary
。
答案 1 :(得分:0)
为什么不直接向控件添加公共方法并返回字典?:
private Dictionary<string, string>
private void updateSE()
{
selectExpr = this.fields1 + " = " + this.fields2;
if (!this.dictionary.ContainsKey(this.fields1))
{
this.dictionary.Add(this.fields1, this.fields2);
}
}
public Dictionary<string, string> GetPairSelections()
{
return this.dictionary;
}