WPF 中的多绑定按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18572836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:33:38  来源:igfitidea点击:

Multibinding in WPF for Button

wpfdata-bindingmultibinding

提问by hrishikesh Deshpande

My application is having 5 text boxes and I want content of them in my ExecuteInsertfunction. right now my Buttoncontains following binding.

我的应用程序有 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"/>

And my ExecuteInsertfunction is as follows. I just want to pass multiple command parameters means(multibinding) can anybody help??

我的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);
   }
}

回答by Nitin

You will have to create the Multivalueconveter for this:

您必须为此创建 Multivalueconveter:

Xaml:

Xml:

converter:

转换器:

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

Button:

按钮:

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

C#

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

you will get the object array in the command handler.

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

Thanks

谢谢

回答by hschne

Your question is not exactly clear, but i would think the simplest way to pass the content of your 5 textboxes to your ExecuteInsert function is to bind each of those textboxes to a property in your viewmodel class and use those properties in your function...

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