繁体   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