Search code examples
c#wpfcomboboxmultibinding

setting c# wpf combobox background color with multibinding


I trying to make my combo box selected value background color based on it self's value and other combo box value, but when debugging in a converter i find that values[0] gets dependency.not set value. so i tried to use Relative Source = self , but then i get this error: "Object reference not set to an instance of an object"

<ComboBox.Background>
    <MultiBinding Converter="{converters:BoolToColorConverter}">
        <Binding RelativeSource="{RelativeSource Mode=Self}" Path="SelectedValue"/>
        <Binding ElementName="cbStdPlate"
            Path="SelectedIndex" />
    </MultiBinding>
</ComboBox.Background>

Anyone could give me a hint ?


Solution

  • Check how you are handling your Converter property. I had reproduced your error and doing it like this worked:

     <Window.Resources>
            <local:Converter x:Key="BoolToColorConverter"/>
     </Window.Resources>
    

    You can replace Window with the Control you are using. And in the Background Multibinding property you call the converter like this:

    <ComboBox.Background>
       <MultiBinding Converter="{StaticResource BoolToColorConverter}">
          <Binding RelativeSource="{RelativeSource Mode=Self}" Path="SelectedValue"/>
          <Binding ElementName="cbStdPlate"
             Path="SelectedIndex" />
       </MultiBinding>
    </ComboBox.Background>
    

    With this if you debug the values[] property of your converter, you will see the selected value as the first position.