WPF Multibinding not working - shortcuts are empty

I am trying to associate two values ​​in the contents of one label with the space in the middle. I am following an example from MSDN (MSDN Article ), but my shortcuts are empty. Here is the code I have:

Grade:

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

Setting the source of elements:

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 fine, for example.

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

So this does not seem to be a problem for getting values.

+4
source share
1 answer

You cannot install MultiBindingwhitout MultiValueConverter.

Try the following:

<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 the 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 bind directly to TextProperty, you really can:

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

Source: https://habr.com/ru/post/1684526/


All Articles