長話短說我有一段應用程序,其中包含一系列綁定到Xaml中的對象實例的列表框。使用IValueConverter
,我能夠從主對象中檢索出<Part>
對象的列表,並顯示檢索到的對象的.ToString()
形式。然而,我想要做的是顯示對象的Name
屬性。我將DisplayMemberPath
設置爲Name
,但結果只顯示listboxitem
。我已經張貼下面的代碼的相關部分:無法讓DisplayMemberPath在列表框中顯示綁定對象數據
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"/>
的ValueConverter:
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;
發現的解決方案
沿着伴侶建議排除故障的線路繼續後,我發現,正在傳遞的對象是部分的對象,而不是ListBoxItems。這導致ListBox
被填充實際對象而不是ListBoxItems。通過更改ValueConverter
以傳遞ListBoxItems的List
並將content標籤設置爲我需要的值,ListBoxes正確填充。我已經在下面的問題區域列出瞭解決方案:
如果調試LocationtoEquipmentCOnverter方法,參數和ANSW呃是對的? ConverterParameter = Front可以轉換爲字符串? – Mate
它確實返回一個包含所有必需屬性的列表(在這種情況下爲單個對象)。該對象甚至顯示在列表框中,如果我不添加displaymemberpath,但顯示繼承(system.MWidget.Part),而不是公共字符串Name屬性。 – ChargerIIC