1

I'm trying to bind a ComboBox to DataContext.

<ComboBox ItemsSource="{Binding Path=Numbers}"
                                SelectedValue="{Binding Path=CurrentNumber,Mode=TwoWay}">
</ComboBox>

The above code works, but when I try to change how the items are displayed using a converter implementing IMultiValueConverter and MultiBinding nothing is displayed. I have debugged the method implementing the IMultiValueConverter and it is not getting executed. What could be the problem?

<ComboBox ItemsSource="{Binding Path=Numbers}"
                                SelectedValue="{Binding Path=CurrentNumber,Mode=TwoWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding Converter="{StaticResource MultiUnitConverter}" ConverterParameter="{x:Static enumerations:Quantity.Length}" >
                        <Binding Path="."/>
                        <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" Path="DataContext.CurrentUnit"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Update:

I tried the following instead of the ComboBox, the converter is fired and the data is loaded but not displayed!

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource MultiUnitConverter}" ConverterParameter="{x:Static enumerations:Quantity.Length}" >
            <Binding Path="CurrentNumber"/>
            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" Path="DataContext.CurrentUnit"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

The following works though:

<TextBlock>
    <TextBlock.Text>
        <Binding Path="CurrentNumber"></Binding>
    </TextBlock.Text>
</TextBlock>
Vahid
  • 5,144
  • 13
  • 70
  • 146

2 Answers2

1

For all who may get stuck with this in the future and ruin their entire evening here is the solution I found!

It seems adding StringFormat solves the problem!

<ComboBox ItemsSource="{Binding Path=Numbers}" SelectedItem="{Binding Path=Number, Mode=TwoWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding 
                                Converter="{StaticResource MultiUnitConverter}" 
                                ConverterParameter="{x:Static enumerations:Quantity.Length}"
                                StringFormat="{}{0:0.###}">
                        <Binding Path="."/>
                        <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" Path="DataContext.CurrentUnit"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
Vahid
  • 5,144
  • 13
  • 70
  • 146
0

Did you define the converter resource somewhere else in your xaml? If not, you should do so. For instance, if your ComboBox lives in a UserControl you could add:

<UserControl.Resources>
    <local:MultiUnitConverter x:Key="multiUnitConverter"/>
</UserControl.Resources>

And of course you would need to update your Converter StaticResource to match the case-sensitive Key above.

Tim Oehler
  • 109
  • 3
  • I think he'd be likely to hear about it if that were the case; I get a XamlParseException on a StaticResource with a nonexistent ResourceKey – 15ee8f99-57ff-4f92-890c-b56153 Jul 13 '16 at 17:33
  • @EdPlunkett The resource is there and I'm not getting any exception. It just doesn't get called! – Vahid Jul 13 '16 at 17:44
  • Are both bindings deifnitely working? E.g. temporarily put two TextBlocks in the template, one with each of the two bindings, do you see what you'd expect? And is the window's viewmodel raising PropertyChanged appropriately? – 15ee8f99-57ff-4f92-890c-b56153 Jul 13 '16 at 17:47
  • @EdPlunkett I'm not changing anything here so should the PropertyChanged get raised? I'm not having problems with my other combo boxes and the PropertyChanged is not raised in those either. Without template the current combo box binds to the source very well. Can you please explain more what you mean by two temporary textboxes? – Vahid Jul 13 '16 at 18:02
  • @Vahid What value does the multibinding get from this binding: ``? – 15ee8f99-57ff-4f92-890c-b56153 Jul 13 '16 at 18:05
  • @EdPlunkett Sorry for answering late. It is Enum, I know the bindings work. I set a breakpoint in the Converter method and when I click on the empty combo box it is called! And it seems it can read the values but somehow not able to display! – Vahid Jul 13 '16 at 18:38
  • @Vahid You mean you've confirmed that the Binding returns the string "Enum", or an enum value? The same as the viewmodel 's CurrentUnit property? "Confirm" doesn't mean "assume" – 15ee8f99-57ff-4f92-890c-b56153 Jul 13 '16 at 18:40
  • @EdPlunkett 1. The `ComboBox` does not display anything. 2. The `ComboBox` actually displays two empty rows! The itemsource consists of two items and this is very odd that it is able to load the data but not be able to display it. 3. Converter is not fired when the Window starts but when I click on the `ComboBox` it is fired yet nothing is displayed. – Vahid Jul 13 '16 at 18:48
  • @Vahid When the converter is fired, please put a breakpoint in the converter's `Convert` method and use the Watch window in the debugger to examine what is in the `object[] values` parameter to `Convert`. – 15ee8f99-57ff-4f92-890c-b56153 Jul 13 '16 at 18:53
  • @EdPlunkett I have done that. They are the values I would get from them Binding. And the converter converts them without any errors and returns. But at the end nothing is displayed! It is very odd! – Vahid Jul 13 '16 at 18:55
  • What does the Convert method return? – 15ee8f99-57ff-4f92-890c-b56153 Jul 13 '16 at 18:56
  • It converts a double number based on the CurrentTarget unit and returns it. – Vahid Jul 13 '16 at 18:57
  • And it's not throwing an exception? – 15ee8f99-57ff-4f92-890c-b56153 Jul 13 '16 at 18:58
  • @EdPlunkett Please look at my updated code, I updated the variable names of itemsource and selectedvalue maybe my bad variable naming made a bit difficult. – Vahid Jul 13 '16 at 19:00
  • Another debugging clue would be to add a 3rd binding where you bind to a property on the View Model that you are certain is raising PropertyChanged (even if the binding is ultimately ignored in your converter). It isn't an ideal solution, but it would shed some light on if the binding is set up incorrectly, or if it's just a timing issue where the binding is evaluating before the DataContext is set. – Tim Oehler Jul 13 '16 at 19:00
  • @EdPlunkett No, I checked it is not throwing any exceptions. And as you see, it is able to load the data when clicked but not able to display them. It has to do something with displaying them! On the other hand the converter is not fired the first time window loads. It might also be converter's fault! – Vahid Jul 13 '16 at 19:02
  • @TimOehler It is able to bind but not fire converter or display them. – Vahid Jul 13 '16 at 19:04