【发布时间】:2020-08-06 17:53:40
【问题描述】:
这是对象类:
public class FileManagement
{
public string FileId { get; set; }
public string FileName { get; set; }
public string TypeofFileName { get; set; }
}
然后,我有一个 FileManagement 对象列表,在我的构造函数中,我按 FileName 对元素进行分组,如下所示
IEnumerable<IGrouping<string, FileManagement>> GroupedList = FileList.GroupBy(p => p.FileName ).OrderBy(x => x.Key);
我将它们分组是因为有更多对象具有相同的FileName 属性,但具有不同的FileId 和TypeofFileName。
我已经声明了IGrouping<string, FileManagement> SelectedFileType,它在下面的 XAML 中的第一个 ComboBox 中使用IGrouping<string, FileManagement> 类型的选定项进行设置。
<ComboBox ItemsSource="{Binding GroupedList }" SelectedItem="{Binding SelectedFileType}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}: {1}">
<Binding Path="Key"/>
<Binding Path="???some sort of string.Join"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox ItemsSource="{Binding SelectedFileType}" DisplayMemberPath="TypeofFileName"></ComboBox>
第二个 ComboBox 包含从第一个 ComboBox 中选择的 IGrouping<string, FileManagement> 中所有元素的 TypeofFileName 属性。这正在按预期工作。
但我还想将以下字符串设置为第一个 ComboBox 的 DisplayMemberPath:
"group_FileName:TypeofFileName1, TypeofFileName2, TypeofFileName3" 其中 group_FileName 基本上是键/组名称,后跟该组中所有元素的 TypeofFileName 属性(如string.Join)。
基本上,我想在 DisplayMemberPath 中放入组名和组元素内联。
我正在考虑使用 MultiBinding 和 string.Join(", ", SelectedFileType.ToList().Select(x => x.TypeofFileName)) 但我不知道如何在绑定中正确使用它。
有没有办法做到这一点?
【问题讨论】:
-
请在您的代码中使用英文成员名称。 XAML 不支持
string.Join之类的东西。