简体   繁体   English

WPF Multibinding无法正常工作 - 标签为空白

[英]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. 我正在关注MSDN( MSDN文章 )中的一个示例,但我的标签是空的。 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: 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 Eg 如果我绑定一个值,它就完美地工作了

             <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. 因此,检索值似乎不是问题。

You cannot set MultiBinding whitout MultiValueConverter . 您无法设置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: 如果您直接绑定到“TextProperty”,您可以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM