使用MultiConverter进行多重绑定会产生DependencyProperty.UnsetValue

时间:2012-12-04 08:33:01

标签: wpf xaml datatrigger multibinding imultivalueconverter

我有MyItem的列表。此对象表示具有某些属性的形状,例如宽度,高度和颜色。使用转换器,我试图将填充颜色从(255,r,g,b)更改为(Alpha,r,g,b)如果我的对象被选中(我不< / em>想要改变不透明度)。 Alpha(double)存在于我的viewmodel中。

我的问题是发送到转换器的两个值都设置为DependencyProperty.UnsetValue,无论我给它什么。如果我只指定Path我希望它绑定到我的viewmodel,但它没有。显然,我在这里做错了。

问:我需要我的viewmodel的AlphaMyItemColor的{​​{1}}的{​​{1}}(我假设在内部时仍然是DataContext MultiBinding块)。我的myitem标签应该是什么样的?

XAML

Binding

转换器

<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>

更新

我解决了。我必须查找父控件(包含项目)才能访问我的viewmodel。 澄清这一点,为什么它好/坏,足以作为答案!我不完全理解发生了什么:)

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();
    }
}

和...

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

0 个答案:

没有答案