c# - WPF 多重绑定(bind)不起作用 - 标签为空白

标签 c# wpf data-binding multibinding

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

类:

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>

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

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

因此检索值似乎不是问题。

最佳答案

您不能设置 MultiBinding whitout 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();
        }
    }

编辑

如果您直接绑定(bind)到“TextProperty”,您实际上可以:

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

关于c# - WPF 多重绑定(bind)不起作用 - 标签为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895712/

相关文章:

c# - Amazon S3,同步,修改日期与上传日期

c# - 如何让 WPF 验证冒泡到父控件?

c# - 如何创建继承 Ellipse 类的子类

wpf - 数据绑定(bind)和锁定列表

data-binding - 绑定(bind)到动态生成的组件/元素的可绑定(bind)属性

c# - Datagrid不更新WPF MVVM中的行

c# - 如何在 .NET 中使用 Google 安全浏览 (v4)

c# - ContainsKey(string key) 如何在 Dictionary<string, string> 上抛出 KeyNotFoundException?

c# - C#什么时候得到显式接口(interface)实现的?

c# - 绑定(bind)到列表框 ItemTemplate 中的矩形?