在WPF中为Button进行多重绑定

时间:2013-09-02 12:03:00

标签: wpf data-binding multibinding

我的应用程序有5个文本框,我希望在ExecuteInsert函数中包含它们的内容。 现在我的Button包含以下绑定。

<Button 
    Content="Add" 
    HorizontalAlignment="Left" 
    Margin="22,281,0,0" 
    VerticalAlignment="Top" 
    Width="75" 
    Command="{Binding Add}" 
    CommandParameter="{Binding ElementName=txtname}" 
    RenderTransformOrigin="1.023,0.765"/>

我的ExecuteInsert功能如下。我只想传递多个命令 参数意味着(多重绑定)任何人都可以帮忙??

private void ExecuteInsert(object obj)
{
   TextBox textbox = obj as TextBox;

   try
   {
      ExecuteConnect(obj);            
      oleDbCommand.CommandText = "INSERT INTO emp(FirstName)VALUES ('" + textbox.Text + "')";
      oleDbCommand.ExecuteNonQuery();
      MessageBox.Show("Data Saved");
   }
   catch (Exception ex)
   {
      MessageBox.Show("ERROR" + ex);
   }
}

2 个答案:

答案 0 :(得分:1)

您必须为此创建Multivalueconveter:

的Xaml:

转换器:

<local:MyConverter x:Key="myConverter" />

按钮:

        <Button>
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource myConverter}">
                    <Binding Path="" ElementName=""/>
                    <Binding Path=""/>
                    <Binding Path=""/>
                </MultiBinding>
             </Button.CommandParameter>
        </Button>

C#

public class MyConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return values;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

您将在命令处理程序中获取对象数组。

由于

答案 1 :(得分:0)

您的问题并不完全清楚,但我认为将5个文本框的内容传递给ExecuteInsert函数的最简单方法是将每个文本框绑定到viewmodel类中的属性,并在函数中使用这些属性...