0

长话短说,我的应用程序有一部分包含一系列绑定到 Xaml 中的对象实例的列表框。使用IValueConverter,我能够<Part>从主对象.ToString()中检索对象列表并显示检索到的对象的形式。但是,我想要做的是显示Name对象的属性。我将 设置DisplayMemberPathName但结果仅将listboxitem. 我已经在下面发布了代码的相关部分:

XAML:

<Window.Resources>
    <local:LocationtoEquipmentCOnverter x:Key="locationEquipmentFiller" />
</Window.Resources>
<Window.DataContext>
    <local:MegaWdiget/>
</Window.DataContext>
<ListBox x:Name="listboxFront" HorizontalAlignment="Left" Margin="180,45,0,0" VerticalAlignment="Top" Width="82" Opacity="0.5" ItemsSource="{Binding ConverterParameter=Front, Converter={StaticResource locationEquipmentFiller}, Mode=OneWay}" DisplayMemberPath="Name"/>

值转换器:

public class LocationtoEquipmentCOnverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        MegaWidget MWidget = (MegaWidget)value;
        Location loc = MWidget.Locations[(string)parameter];
        return loc.Contents;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

MegaWidget 对象包含以下内容:

[XmlElement]
public Dictionary<string, Location> Locations { get; set; }

位置对象包含一个列表,其中包含我需要查询其名称的实际对象:

 public List<Part> Contents;

找到解决方案

在继续按照 Mate 推荐的故障排除路线后,我发现传递的对象是 Part 对象而不是 ListBoxItems。这导致ListBox填充实际对象而不是 ListBoxItems。通过更改ValueConverter传递ListListBoxItems 并将内容标记设置为我需要的内容,ListBoxes 可以正确填充。我在下面的问题区域中列出了解决方案:

4

3 回答 3

2

在继续按照 Mate 推荐的故障排除路线后,我发现传递的对象是 Part 对象而不是 ListBoxItems。这导致 ListBox 被实际对象而不是 ListBoxItems 填充。通过更改 ValueConverter 以传递 ListBoxItems 列表并将内容标记设置为我需要的内容,ListBoxes 可以正确填充。

新的 ValueConverter:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    MegaWidget MWidget = (MegaWidget)value;
    Location loc = MWidget.Locations[(string)parameter];
    List<ListBoxItem> displayContent = new List<ListBoxItem>();

    foreach (Part item in loc.Contents)
    {
        ListBoxItem lbi = new ListBoxItem();
        lbi.Content = item.Name;
        displayContent.Add(lbi);
    }

    return displayContent;
}
于 2012-10-07T14:38:03.217 回答
1

根据您的回复“如果我不添加 DisplayMemberPath,但显示继承 (system.MWidget.Part)”,我想属性 Name 为空。

要检查,请测试:

public class LocationtoEquipmentCOnverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,   System.Globalization.CultureInfo culture)
    {
        MegaWidget MWidget = (MegaWidget)value;
        Location loc = MWidget.Locations[(string)parameter];

        //Refers to Class "Content" used in loc.Contents collection. I do not know what the name that you have used
        foreach (Content item in loc.Contents)
        {
            item.Name += "***";
        }

        return loc.Contents;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2012-10-07T07:33:56.343 回答
0

这里有一个例子howto

  • 模型

    public class MegaWidget
    {
        public Dictionary<string, Location> Locations { get; set; }
    }
    
    public class Location
    {
        public List<Part> Contents { get; set; }
    }
    
    public class Part
    {
        public int Id { get; set; }
        public string PartName { get; set; }
    }
    
  • 转换器

    public class LocationEquipmentConverter : IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            MegaWidget widget = value as MegaWidget;
            string location = (string) parameter;
            return widget?.Locations[ location ]?.Contents;
        }
    
        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            throw new NotImplementedException( );
        }
    }
    
  • 视图模型

    public class FooViewModel : ViewModelBase
    {
        public FooViewModel()
        {
            Widget = new MegaWidget
            {
                Locations = new Dictionary<string, Location>
                {
                    [ "Front" ] = new Location
                    {
                        Contents = new List<Part>
                        {
                            new Part { Id = 1, PartName = "Part 01" },
                            new Part { Id = 2, PartName = "Part 02" },
                            new Part { Id = 3, PartName = "Part 03" },
                        }
                    },
                    [ "Back" ] = new Location
                    {
                        Contents = new List<Part>
                        {
                            new Part { Id = 11, PartName = "Part 11" },
                            new Part { Id = 12, PartName = "Part 12" },
                            new Part { Id = 13, PartName = "Part 13" },
                        }
                    },
                }
            };
        }
    
        public MegaWidget Widget { get; }
    
        #region Property FrontPart
        private Part _frontPart;
    
        public Part FrontPart
        {
            get { return _frontPart; }
            set { SetProperty( ref _frontPart, value ); }
        }
        #endregion
    
        #region Property BackPart
        private Part _backPart;
    
        public Part BackPart
        {
            get { return _backPart; }
            set { SetProperty( ref _backPart, value ); }
        }
        #endregion
    }
    
  • 看法

    <Window x:Class="WPG.WpfApp.FooView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:ViewModel="clr-namespace:WPG.WpfApp.ViewModel"
            xmlns:local="clr-namespace:WPG.WpfApp"
            mc:Ignorable="d"
            Title="FooView" Height="300" Width="300">
        <Window.Resources>
            <ViewModel:LocationEquipmentConverter x:Key="LocationEquipmentConverter"/>
        </Window.Resources>
        <Window.DataContext>
            <ViewModel:FooViewModel/>
        </Window.DataContext>
        <Grid>
            <StackPanel>
                <ListBox ItemsSource="{Binding Widget, ConverterParameter=Front, Converter={StaticResource LocationEquipmentConverter}, Mode=OneWay}"
                         SelectedItem="{Binding Path=FrontPart}"
                         DisplayMemberPath="PartName"/>
                <ListBox ItemsSource="{Binding Widget, ConverterParameter=Back, Converter={StaticResource LocationEquipmentConverter}, Mode=OneWay}"
                         SelectedItem="{Binding Path=BackPart}"
                         DisplayMemberPath="PartName"/>
            </StackPanel>
        </Grid>
    </Window>
    
  • 截屏

在此处输入图像描述

于 2017-02-11T07:56:12.740 回答