Search code examples
c#wpfdata-bindingmultibinding

WPF Multibinding not working - Labels are blank


I'm trying to bind two values into the content of one label with a space in the middle. I'm following an example from MSDN (MSDN Article) but my labels are empty. Here is the code I have:

Class:

public class Item
{
    //Other properties removed to shorten
    public string name { get; set; }
    public string typeLine { get; set; }
}

Setting the items source:

ItemsDisplay.ItemsSource = searchResults;

XAML:

<ItemsControl Name="ItemsDisplay">
     <ItemsControl.ItemTemplate>
         <DataTemplate>

             <Grid>
                 <!-- COLUMN DEFINITIONS ETC REMOVED TO SHORTEN -->

                 <StackPanel Grid.Column="1">
                     <Label Name="ItemName" Margin="10">
                         <Label.Content>
                             <MultiBinding StringFormat="{}{0} {1}">
                                 <Binding Path="name" />
                                 <Binding Path="typeLine" />
                             </MultiBinding>
                         </Label.Content>
                     </Label>

                 </StackPanel>

             </Grid>

         </DataTemplate>
     </ItemsControl.ItemTemplate>
</ItemsControl>

If I bind a single value it works perfectly E.g.

             <StackPanel Grid.Column="1">
                 <Label Name="ItemName" Margin="10" Content="{Binding Path=name}" />
                 <Label Name="ItemType" Margin="10" Content="{Binding Path=typeLine}" />
             </StackPanel>

So it doesn't appear to be a problem retrieving the values.


Solution

  • You cannot set MultiBinding whitout MultiValueConverter.

    Try this:

    <ItemsControl Name="ItemsDisplay">
        <ItemsControl.Resources>
            <local:MyMultiConv x:Key="MyConv"/>
        </ItemsControl.Resources>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <!-- COLUMN DEFINITIONS ETC REMOVED TO SHORTEN -->
                            <StackPanel Grid.Column="1">
                                <Label Name="ItemName" Margin="10">
                                    <Label.Content>
                                        <MultiBinding  Converter="{StaticResource MyConv}">
                                            <Binding Path="name" />
                                            <Binding Path="typeLine" />
                                        </MultiBinding>
                                    </Label.Content>
                                </Label>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    And converter:

    public class MyMultiConv : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                return string.Format("{0} {1}", values[0], values[1]);
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    Edit

    If you binding to directly "TextProperty" you actualy can:

    <Textblock Name="ItemName" Margin="10">
            <Textblock.Text>
                    <MultiBinding  StringFormat="{}{0} {1}">
                        <Binding Path="name" />
                        <Binding Path="typeLine" />
                   </MultiBinding>
            </Textblock.Text>
    </Textblock>