繁体   English   中英

用户控件中的自定义方法

[英]Custom method in user control

我有一个用户控件(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();
    }
}
}

添加子项

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++;
        }
    }
}

如果您想编写有效的WPF,则应该停止尝试使用它,就像Windows Forms一样。WPF都是关于数据绑定的。 您应该查看MSDN上的“ 数据绑定概述”页面以进行深入介绍。

为了回答您的问题, 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>

然后,您可以从UserControl内部和外部Bind到此属性:

<YourXmlNamespace:ItemPairing Results="{Binding YourExternalResultsProperty}" />

更新>>>

您可以通过将主视图模型设置为MainWindowDataContext来处理从多个UserControl聚合Results集合的情况。 该视图模型可以包含多个属性,一个属性可以绑定到每个UserControl Results属性,也可以包含您可能使用的任何其他视图模型类型的属性。

无论哪种方式,通过使用这样的主视图模型,您都应该具有一个包含绑定到所有Results集合的属性的类(无论是否在其他视图模型中),这样您就可以访问它们。 然后,您可以使用一种方法,根据所有其他值创建一个新的Dictionary

为什么不只向您的控件添加一个公共方法并返回一个字典呢?:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM