WPF 多重绑定不起作用 - 标签为空白

WPF Multibinding not working - Labels are blank

我试图将两个值绑定到一个中间带有 space 的标签的内容中。我正在按照 MSDN (MSDN Article) 中的示例进行操作,但我的标签是空的。这是我的代码:

Class:

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

正在设置项目来源:

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>

如果我绑定一个值,它会完美地工作例如

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

因此检索值似乎没有问题。

您无法设置 MultiBinding 而没有 MultiValueConverter

试试这个:

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

和转换器:

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

编辑

如果您直接绑定到 "TextProperty",您实际上可以:

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