2013-06-05 34 views
0

我有一個包含ImageLabel(與XAML設計)一類PC,我想獲得電腦列表中ListBox其他類。使用XAML添加自定義類的ListBox中的C#WPF

我想這一點,但我得到的錯誤System.Windows.Markup.XamlParseException

pc p = new pc(); 
list_pc.Items.Add(p); 
(where list_pc is a ListBox) 

這是XAML單個PC

<Window 
    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" mc:Ignorable="d" 
    x:Class="TnCyberCafe.pc" 
    Title="pc" 
    SizeToContent="WidthAndHeight" 
    ShowInTaskbar="False" 
    WindowStartupLocation="CenterScreen" 
    WindowStyle="None" 
    AllowsTransparency="True" 
    Background="Transparent" Width="95" Height="104.982"> 

    <Grid HorizontalAlignment="Left" Margin="0,10,-15,10" Width="110"> 
     <Image x:Name="image" HorizontalAlignment="Left" Height="96" VerticalAlignment="Top" Width="100" Source="Resources/aaa.png" RenderTransformOrigin="0.5,0.26" Margin="0,-16,0,0"/> 
     <Label Content="Label" HorizontalAlignment="Left" Height="25" Margin="20,70,0,-10" VerticalAlignment="Top" Width="45"/> 
    </Grid> 
</Window> 

這是XAMLlist_pc

<ListBox x:Name="liste_pc" ScrollViewer.VerticalScrollBarVisibility="Disabled"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel IsItemsHost="True" Orientation="Horizontal"/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
    List PCs     
</ListBox> 
+1

HUH的觀察集合?您是否可以編輯您的問題以包含有關錯誤發生位置以及「xaml設計」的外觀的更多上下文? –

+0

當我嘗試將PC添加至列表框中我得到的錯誤 – user2456665

+1

我認爲它可以與的ObservableCollection 做,但我不知道如何exctly :) – user2456665

回答

0

您的第一個問題stion有關System.Windows.Markup.XamlParseException:

由於Garry Vass提到出了問題,但並沒有告訴你到底發生了什麼事。如果您正在Visual Studio中開發,請點擊例外調試選項卡並啓用公共語言運行時例外。這會指出錯誤代碼。

對於第二個問題,你應該有數據綁定和列表框的ItemTemplate如下

<ListBox x:Name="liste_pc" ItemsSource="{Binding PCList}" ScrollViewer.VerticalScrollBarVisibility="Disabled"> 
    <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <Image Source="{Binding PCImageSource}" /> 
        <Label Content="{Binding Path=PCName}" /> 
       </StackPanel> 
      </DataTemplate> 
    </ListBox.ItemTemplate> 

</ListBox> 

其中PCList是PC類對象的項目

+0

是的,它是工作,但設計是缺少在這裏,它只是在地方的圖像和標籤複製而不XAML設計敵人每一個?如何解決? – user2456665

+0

你能解釋一下你的實際需求嗎?你想在UI中展示什麼?你是否遵循mvvm patterm? – Jasti

+0

在我的UI我希望在我的實例級PC,你的代碼我得到的所有對象,我把他們在的地方,而不在課堂PC.xaml創建XAML設計得到我的電腦所有的XAML設計 – user2456665