1

I have a button with some multibinding to which is attached a command :

<Button Content="remove" HorizontalAlignment="Right" VerticalAlignment="Top" Cursor="Hand" Focusable="False">
  <Button.Command>
      <Binding  Path="DataContext.DeleteColumnCommand"  ElementName="treeView" />
  </Button.Command>
  <Button.CommandParameter>
      <MultiBinding  Converter="{StaticResource midConverter}">
              <Binding Path="Text"  ElementName="tableName"/>
              <Binding Path="Name" />
      </MultiBinding>
  </Button.CommandParameter>
</Button>

I see that when I put a breakpoint in the converter, every value is set and it looks like it is working.

However, when actually called on the command, I receive as argument an array populated with nulls !

I imagine that WPF reuses and mutates the array I saw in my converter, which does not make nonsense as this is a reference type which I have not allocated and in the context of WPF max performance is much needed.

My question is : what best summarizes the guidance/guarantees around mutation like that in WPF ?

Is there a rule around this ?

PS : I see here that other people had the same problem and did not understand the origin apparently.

PPS : I did not make my question clear enough may be, but it follows naturally that one has to allocate a new structure, list, array, whatever, on the heap, as the one you get might be reused. The question is : from this ad-hoc example, what are the rules for WPF in such cases ?

Community
  • 1
  • 1
nicolas
  • 9,549
  • 3
  • 39
  • 83

1 Answers1

0

here is an answer to a similar question

you can use Multibinding and a Converter

<Button Content="Add" Command="{Binding AddCommand}"
 <Button.CommandParameter>
    <MultiBinding Converter="{StaticResource YourConverter}">
         <Binding Path="Text" ElementName="txt1"/>
         <Binding Path="Text" ElementName="txt2"/>
    </MultiBinding>
 </Button.CommandParameter>
</Button>

converter: its important to create a new array!

public class YourConverter : IMultiValueConverter
{
 public object Convert(object[] values, ...)
 {
    //.Net > 4.0
    return new Tuple<int, int>((int)values[0], (int)values[1]); //<-- this is important

    //or .Net < 4.0
    //return values.ToArray();//<-- this is important
 }

 ...
}

command

private void  CommandExecute(object parameter)
{
    var o= (Tuple<int, int>)parameter;
    var a= o.Item1;
    var b= o.Item2;
    Calculater calcu = new Calcu();
    int c = calcu.sum(a, b);      
}

ps: pls check my syntax - its written from my mind...

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • indeed, creating a new allocated structure follows naturally from what I exposed. my question is : when can we safely reuse data given by WPf ? I imagine everytime we get a reference type we have to copy.... except for value object ? – nicolas Apr 05 '13 at 08:19