【问题标题】:ComboBox DisplayMemberPath containing LINQ group name and group elements inlineComboBox DisplayMemberPath 包含 LINQ 组名称和内联组元素
【发布时间】: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 属性,但具有不同的FileIdTypeofFileName

我已经声明了IGrouping&lt;string, FileManagement&gt; SelectedFileType,它在下面的 XAML 中的第一个 ComboBox 中使用IGrouping&lt;string, FileManagement&gt; 类型的选定项进行设置。

<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&lt;string, FileManagement&gt; 中所有元素的 TypeofFileName 属性。这正在按预期工作。

但我还想将以下字符串设置为第一个 ComboBox 的 DisplayMemberPath: "group_FileName:TypeofFileName1, TypeofFileName2, TypeofFileName3" 其中 group_FileName 基本上是键/组名称,后跟该组中所有元素的 TypeofFileName 属性(如string.Join)。
基本上,我想在 DisplayMemberPath 中放入组名和组元素内联。

我正在考虑使用 MultiBinding 和 string.Join(", ", SelectedFileType.ToList().Select(x =&gt; x.TypeofFileName)) 但我不知道如何在绑定中正确使用它。 有没有办法做到这一点?

【问题讨论】:

  • 请在您的代码中使用英文成员名称。 XAML 不支持 string.Join 之类的东西。

标签: c# wpf linq xaml


【解决方案1】:

XAML 不支持调用诸如string.Join 之类的函数,但您可以绑定到例如SelectedObiectGeneral 并使用调用string.Join 并返回string 的转换器:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding>
            <MultiBinding.Converter>
                <local:YourConverter />
            </MultiBinding.Converter>
            <Binding Path="Key"/>
            <Binding Path="SelectedObiectGeneral"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

YourConverter 只是一个实现IValueConverterIMultiValueConverter 的类,具体取决于您在XAML 标记中使用Binding 还是MultiBinding

【讨论】:

  • 你指出了我正确的方向,我做了一个简单的 IValueConverter
猜你喜欢
  • 2021-12-30
  • 2023-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多