列表框项目放在彼此之上

时间:2012-08-08 21:20:53

标签: c# wpf

我似乎无法弄清楚为什么列表框项目放在彼此的顶部......它们应该在彼此之下。下面是一些标记和代码。

<ListBox x:Name="DeviceList" Background="#ff4c4c4c" BorderThickness="0" ScrollViewer.CanContentScroll="False" MouseEnter="DeviceList_MouseEnter" MouseLeave="DeviceList_MouseLeave" 
                     ManipulationBoundaryFeedback="DeviceList_ManipulationBoundaryFeedback" ItemContainerStyle="{DynamicResource ResourceKey=ListBoxItemStyle}" PreviewMouseDown="DeviceList_PreviewMouseDown" 
                     PreviewMouseMove="DeviceList_PreviewMouseMove" PreviewMouseUp="DeviceList_PreviewMouseUp" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DockPanel.Dock="Bottom">
                <ListBox.Resources>
                    <ResourceDictionary>
                        <ResourceDictionary.MergedDictionaries>
                            <ResourceDictionary Source="..\Utilities\Resources\Themes\Slider.xaml"></ResourceDictionary>
                        </ResourceDictionary.MergedDictionaries>
                    </ResourceDictionary>
                </ListBox.Resources>
            </ListBox>

#region Constructors
    /// <summary>
    /// Constructor.
    /// </summary>
    public ConfiguratorView()
    {
        InitializeComponent();

        foreach (Device device in (Application.Current.Windows[1].DataContext as ConfiguratorViewModel).AvailableDevices)
        {
            devices.Add(AddDevice(device.Icon + "_def", device.Description));
        }

        DeviceList.ItemsSource = devices;
    }
    #endregion


    #region Internal Members
    /// <summary>
    /// Add the device to the list of devices.
    /// </summary>
    /// <param name="icon"></param>
    /// <param name="description"></param>
    /// <returns></returns>
    private Canvas AddDevice(string icon, string description)
    {
        Canvas canvas = new Canvas();
        canvas.Name = icon;

        ContentControl backgroundContent = new ContentControl();
        Label label = new Label();

        backgroundContent.Template = Application.Current.FindResource(icon) as ControlTemplate;
        label.Content = description;

        canvas.Children.Add(backgroundContent);
        canvas.Children.Add(label);

        return canvas;
    }

设备列表将画布添加为项目...然后我将ItemsSource设置为List。加载它会在最后一个图标的顶部显示所有图标。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

因为Canvas.Top默认为NaN,所有内容都会显示在彼此之上。

您可以手动计算相应的Canvas.Top值,但我建议:

  1. 使用Description和icon

  2. 的属性保持Device对象的简单性
  3. 为ListBox项创建DataTemplate以显示这些属性。

  4. 编辑:

    例如(我没有测试过这个)

    假设您的Device类看起来像这样:

    public class Device
    {
        public string Description { get; set; }
        public object Icon { get; set; } 
    }
    

    然后,ListBox的数据完整性可能如下所示:

    <ListBox ItemsSource="{Binding Devices}">
        <ListBox.ItemsTemplate>
            <DataTemplate>
                <Canvas>
                    <!-- add items here -->
                    <TextBlock Text="{Binding Description}" Canvas.Top="5" />
                <Canvas>
            </DataTemplate>
        </ListBox.ItemsTemplate>
    </ListBox>