WPF MultiValueConverter问题 [英] WPF MultiValueConverter problem

查看:51
本文介绍了WPF MultiValueConverter问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在使用MultiBinding作为Button.CommandParameter时遇到了一个主要问题.在我的应用程序中,我有四个textBoxes,其内容应用作Button.Command的参数.为此,我在XAML文件中执行了以下操作:

Hi,

I have encountered a major problem with MultiBinding as Button.CommandParameter. In my application I have four textBoxes which contents should be used as a parameter for the Button.Command. For this purpose I''ve done something like this inn the XAML file:

<Button Grid.Row="0" Grid.Column="2" x:Name="btnUpdate" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="5,40,5,0" Content="Update product" Command="{Binding Path=Update_Command}">
    <Button.CommandParameter>
        <MultiBinding Converter="{StaticResource ResourceKey=PVMObj}">
            <Binding ElementName="txtID" Path="Text" /> <!-- TextBox with the ID -->
            <Binding ElementName="txtName" Path="Text" /> <!-- TextBox with the Name -->
            <Binding ElementName="txtPrice" Path="Text" /> <!-- TextBox with the Price -->
            <Binding ElementName="txtReseller" Path="Text" /> <!-- TextBox with the reseller name -->
        </MultiBinding>
    <Button.CommandParameter>
</Button>


如您所见,我已经将必需的MultiValueConverter对象(PVMObj)绑定到了MultiBinding. PVM的实现如下所示:


As you can see I have the necessary MultiValueConverter Object (PVMObj) bound to MultiBinding. And the PVM''s implementation looks like this:

class PVM : IMultiValueConverter
{
    #region IMultiValueConverter Members
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(values != null)
        {
            return new Product(ToInt32(values[0]), values[1].ToString(), ToDouble(values[2]), values[3].ToString());
            // Exception text is not in correct format!
        }
        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
    #endregion
}


为了调试,我对PMV进行了如下修改:


For debugging purposes I''ve modified the PMV as follows:

class PVM : IMultiValueConverter
{
    #region IMultiValueConverter Members
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(values != null && values[3].ToString().Length > 0)
        {
            string s = values[0]+"-"+values[1]+"-"+values[2]+"-"+values[3];
            System.Windows.MessageBox.Show(s);

            // Designer Crashes with Unreported Exception in the following line.
            return new Product(s);
            // Application works but the teamLead says the designer should work I must resolve this issue...
        }
        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
    #endregion
}


在这种情况下,将发生以下情况:首先出现一个消息框,其中包含以下内容:
"1---"
然后我关闭此消息框,然后弹出另一个消息框:
"1-样本--"
再来一次
"1-Sample-19.95-"
最后一次:
"1-Sample-19.95-MediaTower"

IMul​​tiValueConverter如何工作?从许多示例中,我看到人们只是在使用object [] values数组,但我不能.有什么想法我做错了吗?


In this case the following scenario occurs: First a Messagebox appears with the following contents:
"1- - - "
Then I close this MessageBox and another one pops up:
"1-Sample- - "
then again
"1-Sample-19.95- "
And for last time:
"1-Sample-19.95-MediaTower"

How does this IMultiValueConverter works? From many examples as I saw people are simply using the object[] values array, yet I can not. Any thoughts what I am doing wrong?

推荐答案

如果您只需要设计师工作,则可以在转换器中添加检查以查看您当前是否在工作在设计器模式下;

If you just need the designer to work, you can add a check in the converter to see if you''re currently in Designer mode;

class PVM : IMultiValueConverter
{
  private static readonly DependencyObject DummyDependencyObject = new DependencyObject();
        
  public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    if (values != null && values[3].ToString().Length > 0)
    {
      string s = values[0] + "-" + values[1] + "-" + values[2] + "-" + values[3];
      if (DesignerProperties.GetIsInDesignMode(DummyDependencyObject))
        return null;
      else
        return s;
    }
    return null;
  }

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



希望这会有所帮助,
弗雷德里克(Fredrik)



Hope this helps,
Fredrik


好吧,主要目标是确定:
1.)为什么我看到更多的消息框而不是一个
2.),我想这将解决如何纠正错误.
Well the main objective would be to determine:
1.) why do I see more Messageboxes instead of just one
2.) and I guess that would resolve how to correct the error.


这篇关于WPF MultiValueConverter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆