【问题标题】:Multibinding in WPF for ButtonWPF中用于按钮的多重绑定
【发布时间】:2013-09-02 12:02:59
【问题描述】:

我的应用程序有 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);
   }
}

【问题讨论】:

    标签: wpf data-binding multibinding


    【解决方案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();
            }
        }
    

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

    谢谢

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 有什么方法可以使用多重绑定吗?
      猜你喜欢
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2010-11-13
      • 1970-01-01
      相关资源
      最近更新 更多