Multibinding in WPF for Button

岁酱吖の 提交于 2020-01-03 06:27:09

问题


My application is having 5 text boxes and I want content of them in my ExecuteInsert function. right now my Button contains following binding.

<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 ExecuteInsert function is as follows. I just want to pass multiple command parameters means(multibinding) can anybody help??

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

回答1:


You will have to create the Multivalueconveter for this:

Xaml:

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#

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




回答2:


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...



来源:https://stackoverflow.com/questions/18572836/multibinding-in-wpf-for-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!