开发者

Converters on child bindings in a MultiBinding

Suppose I have this MultiBinding:

<MultiBinding Converter="{StaticResource FooBarConverter}>
  <Binding Path="Foo" Converter="{StaticResource FooConverter}" />
  <Binding Path="Bar" Converter="{StaticResource BarConverter}" />
</MultiBinding>

This doesn't seem to work: the values array passed to FooBarConverter contains DependencyProperty.UnsetValue for each value (two, in this case). Removing the converters on the chi开发者_运维技巧ld bindings (FooConverter and BarConverter) gives me the actual values. By the way: those converters are properly invoked, it just looks like their result is discarded.

Is this intended behavior? I want to bind 2 properties by I need to convert at least one of them before throwing them into the MultiValueConverter...


The developers in Kishore's shared link came to the conclusion that to make such a MultiBinding, the child Bindings must return the same type of result as the parent MultiBinding. So, in my case, if I wanted the parent MultiBinding to return a value of type Visibility, the child Bindings must also return Visibility values. Not doing so will pass UnsetValues to your converter method, likely giving you undesireable results.

Here is a snippet of code that works for me. Note that Converters "VisibleIfTrue" and "EnumToVisibility" both return type Visibility values:

<Grid.Visibility>
    <MultiBinding Converter="{StaticResource MultiVisibilityConverter}">
        <Binding Path="JobHasData" Converter="{StaticResource VisibleIfTrue}" />
        <Binding Path="CurrentMode" Converter="{StaticResource EnumToVisibility}" ConverterParameter="{x:Static Mode.Setup}" />
    </MultiBinding>
</Grid.Visibility>

It is annoying that you can't pass it different value types to process and give you the result you want. (I initially tried to pass bools to the converter.)

Hope this helps anyone who waited the seven years for the answer. ;)


you have mention converter in the Multibinding tag like this

<TextBlock Grid.Row="3" Grid.Column="1" Padding="5">
    <TextBlock.Text>
      <MultiBinding Converter="{StaticResource sumConverter}">
        <Binding  Path="FirstNum" />
        <Binding  Path="SecondNum" />
        <Binding   Path="ThirdNum" />
      </MultiBinding>
    </TextBlock.Text>
  </TextBlock>


If WPF was prior to 4.0 then it is a known and fixed bug that have workaround.

Here located a sample implementation of the workaround for poor man that are forced to work with elder versions.

To say shortly, old wpf versions are trying to convert values from multibinding child bindings that have converters directly into type of target dependency property. Workaround is to create hidden label, move multibinding or its child binding converter to label.content as it expects object and then bind desired property to it.


public class DataClass
{
   public string FirstName { get; set; }

   public string Surname { get; set; }
}

public class NameMultiValueConverter : IMultiValueConverter
{
   public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
       return String.Format("{0} {1}", values[0], values[1]);
   }
}

The XAML looks basically like this

<Window xmlns:local="clr-namespace:BlogIMultiValueConverter">
<Window.Resources>
    <local:NameMultiValueConverter x:Key="NameMultiValueConverter" />
</Window.Resources>
<Grid>
    <TextBox Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" />
    <TextBox Text="{Binding Path=Surname, UpdateSourceTrigger=PropertyChanged}" />
    <TextBlock>
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource MultiValueConverter}">
                <Binding Path="FirstName" />
                <Binding Path="Surname" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
</Grid>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜