2012-10-07 22 views
0

長話短說我有一段應用程序,其中包含一系列綁定到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正確填充。我已經在下面的問題區域列出瞭解決方案:

+0

如果調試LocationtoEquipmentCOnverter方法,參數和ANSW呃是對的? ConverterParameter = Front可以轉換爲字符串? – Mate

+0

它確實返回一個包含所有必需屬性的列表(在這種情況下爲單個對象)。該對象甚至顯示在列表框中,如果我不添加displaymemberpath,但顯示繼承(system.MWidget.Part),而不是公共字符串Name屬性。 – ChargerIIC

回答

2

一起排除故障由伴侶推薦線路繼續後,我發現,正在傳遞的對象是部分的對象,而不是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; 
} 
+0

做得好ChargerIIC! 。如果您想提供符合「最佳實踐」的解決方案,則還需要顯示類聲明部分。 – Mate

+0

雖然這是工作,它從MVVM POV是錯誤的 –

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(); 
    } 
} 
+0

嘗試了此操作,但ListBoxItem仍爲空。我可以在調試過程中四處鑽研,看到一個完整的列表(inlcuding +字符串名稱字段)正在發送回UI,但ListBoxItem保持空白。我也嘗試將DisplayMemberPath設置爲對象的其他屬性:重量,插槽等,但仍然沒有顯示。 – ChargerIIC

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> 
    
  • 截圖

enter image description here

相關問題