簡體   English   中英

在ListBox C#WPF中使用XAML添加自定義類

[英]Adding custom class with XAML in ListBox C# WPF

我有一個類PC包含ImageLabel (與XAML設計),我想獲得電腦的名單ListBox在其他類。

我想這一點,但我得到的錯誤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>

關於System.Windows.Markup.XamlParseException的第一個問題:

正如Garry Vass提到的那樣,出了點問題,但沒有確切告訴您發生了什么。 如果您在Visual Studio中進行開發,請在“ 調試”選項卡下單擊“ 異常” ,然后啟用“ 公共語言運行時異常” 這會將您指向錯誤代碼。

對於第二個問題,您應該具有如下所示的數據綁定和ListBox 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類對象項的可觀察集合

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM