我试图将两个值绑定到一个标签的内容中,中间有一个空格。我来自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>
如果我绑定单个值,它可以很好地工作,例如
<StackPanel Grid.Column="1">
<Label Name="ItemName" Margin="10" Content="{Binding Path=name}" />
<Label Name="ItemType" Margin="10" Content="{Binding Path=typeLine}" />
</StackPanel>
因此,检索值似乎不成问题。
答案 0 :(得分:3)
您无法设置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();
}
}
修改强>
如果您直接绑定&#34; TextProperty&#34;你真的可以:
<Textblock Name="ItemName" Margin="10">
<Textblock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="name" />
<Binding Path="typeLine" />
</MultiBinding>
</Textblock.Text>
</Textblock>