【问题标题】:Custom method in user control用户控件中的自定义方法
【发布时间】:2013-10-23 19:32:18
【问题描述】:

我有一个包含两个组合框和一个按钮的用户控件 (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++;
        }
    }
}

【问题讨论】:

    标签: c# wpf xaml methods user-controls


    【解决方案1】:

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

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

    【讨论】:

    • 这是一个中肯的答案,比公认的答案更好。虽然接受的答案是很好的建议,但这个答案符合问题的期望。
    【解决方案2】:

    如果您想编写有效的 WPF,那么您应该停止尝试像使用 Windows 窗体一样使用它... 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&lt;string, string&gt;

    那么你可以从UserControl内外Bind到这个属性:

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

    更新>>>

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

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

    【讨论】:

    • 感谢 Sheridan,这是我一开始的方式,但无法让它按照我想要的方式工作。如您所知,这是我第一次尝试创建 UserControl。由于将有多个 ItemPairing 控件,将所有这些结果组合到一个字典中的最佳方法是什么(遍历每个 ItemPairing 并获取结果,或者有没有办法通过绑定来处理它)?这就是我之前被难住的地方,也是为什么我认为我需要退回到公共方法(我也可以只阅读结果而不是公共方法)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 2011-10-13
    相关资源
    最近更新 更多