1

I have a list of MyItem. This object represents a shape with some properties, such as width, height and color. Using a converter, I'm trying to change the fill color from (255, r, g, b) to (Alpha, r, g, b) if my object is selected (I do not want to change the opacity). Alpha (double) lives inside my viewmodel.

My problem is that both values sent to the converter are set to DependencyProperty.UnsetValue, no matter what I give to it. If I specify only the Path I would expect it to bind to my viewmodel, but it doesn't. Obviously, I'm doing something wrong here.

Q: I need the Alpha of my viewmodel and the MyItemColor of the DataContext for myitem (which I would assume is still the DataContext when inside the MultiBinding block). What should my Binding tags look like?

XAML

<DataTemplate>
  <Grid>
    <Rectangle x:Name="myitem" Width="{Binding MyItemWidth}" Height="{Binding MyItemHeight}" />
  </Grid>
  <DataTemplate.Triggers>
    <MultiDataTrigger>
      <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding IsSelected}" Value="False"/>
      </MultiDataTrigger.Conditions>
      <Setter TargetName="myitem" Property="Fill">
        <Setter.Value>
          <!-- Simple binding works. WHAT is the difference?
          <SolidColorBrush>
            <SolidColorBrush.Color>
              <Binding Path="Color" />
            </SolidColorBrush.Color>
          </SolidColorBrush>
          -->
          <SolidColorBrush>
            <SolidColorBrush.Color>
              <!-- Change fill color if DataContext.Scale changes -->
              <MultiBinding Converter="{StaticResource DoubleToColorConverter}">
                <!-- wrong? -->
                <Binding Path="Alpha" />
                <!-- wrong? -->
                <Binding ElementName="myitem" Path="MyItemColor" />
              </MultiBinding>
            </SolidColorBrush.Color>
          </SolidColorBrush>
        </Setter.Value>
      </Setter>
    </MultiDataTrigger>
  </DataTemplate.Triggers>
</DataTemplate>

Converter

public class DoubleToColorConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
        object parameter, CultureInfo culture)
    {
        // values[0] == DependencyProperty.UnsetValue
        // values[1] == DependencyProperty.UnsetValue

        return null; // New color
    }

    public object[] ConvertBack(object value, Type[] targetTypes,
        object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

UPDATE

I solved it. I had to look for the parent control (which contains the items) in order to get access to my viewmodel. A clarification of this, why it's good/bad, suffices as an answer! I don't fully understand what's going on :)

<MultiBinding Converter="{StaticResource DoubleToColorConverter}">
  <Binding ElementName="myItemsControl" Path="DataContext.Alpha"/>
  <Binding Path="Color" />
</MultiBinding>

And...

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
        return Colors.Transparent;
    ...
}
l33t
  • 18,692
  • 16
  • 103
  • 180
  • This is part of the answer: http://stackoverflow.com/questions/6273083/why-does-multibinding-with-a-converter-not-work-within-a-tooltip – l33t Dec 04 '12 at 09:00
  • Please see my update. I solved it, but a clarification of what is going on is needed. Thanks. – l33t Dec 04 '12 at 09:58

0 Answers0