cimbalino / Cimbalino-Phone-Toolkit

Cimbalino Windows Phone Toolkit
MIT License
78 stars 32 forks source link

Can't get value in MultiBindingBehavior #14

Closed sun8134 closed 11 years ago

sun8134 commented 11 years ago

I used MultiBindingBehavior in wp7.5, the xaml is here:

            <ListBox Height="510" Name="lb1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Width="432" Orientation="Horizontal">
                            <TextBlock TextWrapping="Wrap" Text="{Binding name}" FontSize="32" Width="200" x:Name="t12" />
                            <TextBlock TextWrapping="Wrap" Text="{Binding number,Mode=TwoWay}" FontSize="32" Margin="0" x:Name="t11" DataContext="{Binding}">
                                    <i:Interaction.Behaviors>
                                        <cimbalinoBehaviors:MultiBindingBehavior Converter="{StaticResource ColorConverter}" PropertyName="Foreground" >
                                            <cimbalinoBehaviors:MultiBindingItem Value="{Binding name}" />
                                            <cimbalinoBehaviors:MultiBindingItem Value="{Binding ElementName=listpicker1, Path=SelectedItem.Content}" />                                                
                                        </cimbalinoBehaviors:MultiBindingBehavior>
                                    </i:Interaction.Behaviors>
                            </TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>                 
            </ListBox>

but in "public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)", the values[0] is null...

how can I get the value in databinding?

Thinks

pedrolamas commented 11 years ago

I assume "name" is a property from your ViewModel, correct? Is it really "name" or should it be "Name"? Take a look in the Output window in Visual Studio and check if there is an Binding error message that may explain why the value is null.

sun8134 commented 11 years ago

yes the "name" is a property of my ViewModel

And I found that in each binding,the "public override object Convert" method will run twice the first time the values[0] is null and the second time it will have the correct value

pedrolamas commented 11 years ago

That is actually expected behavior, as PropertyChanged events get raised each time a property value changes.

In this case, if the MultiBindingBehavior contains 2 bindings, it will raise the Convert() method twice once per each PropertyChanged event, and at startup, I guess a PropertyChanged event is raised for each of the binded properties.