带有DataTemplate绑定问题的WPF列表框

时间:2013-01-02 06:24:19

标签: wpf binding listbox datatemplate

我正在尝试使用自定义数据模板创建ListBox,但绑定似乎是错误的。当我尝试使用 PacketItem 模板将项目添加到列表时,会添加它们,但三个 HexItem 文本框中的文本仍为空白。当我将列表框的项目模板直接设置为 HexItem 数据模板时,工作正常。嵌套在 PacketItem 数据模板中时,它只会出现问题。

我确定这是一个简单的修复,似乎当模板嵌套时,它不知道在哪里查找文本绑定。我正在考虑绑定到相对源来从父级获取数据上下文会起作用,但这似乎是错误的方法。

修订XAML

<UserControl x:Class="wpfPacketBox.ctlPacketBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:wpfPacketBox"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="600">
    <UserControl.Resources>
        <DataTemplate x:Key="PacketItem">
            <Expander IsExpanded="True"
                      d:DesignWidth="600">
                <Expander.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="PacketNum" />
                            <TextBlock Text="Source" />
                            <TextBlock Text="Dest" />
                            <TextBlock Text="PacketID (FF0A)" />
                        </StackPanel>
                    </DataTemplate>
                </Expander.HeaderTemplate>
                <Expander.ContentTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="10*" />
                                <ColumnDefinition Width="65*" />
                                <ColumnDefinition Width="25*" />
                            </Grid.ColumnDefinitions>

                            <Grid Grid.Column="0">
                                <TextBox Text="{Binding Offset}"
                                         FontFamily="Courier New"
                                         Foreground="Blue"
                                         Background="#FFCCCCCC"
                                         TextWrapping="Wrap"
                                         BorderThickness="0" />
                            </Grid>

                            <Grid Grid.Column="1">
                                <TextBox Text="{Binding Hex}"
                                         FontFamily="Courier New"
                                         TextWrapping="Wrap"
                                         BorderThickness="0" />
                            </Grid>

                            <Grid Grid.Column="2">
                                <TextBox Text="{Binding Ascii}"
                                         FontFamily="Courier New"
                                         Background="#FFCCCCCC"
                                         TextWrapping="Wrap"
                                         BorderThickness="0" />
                            </Grid>
                        </Grid>
                    </DataTemplate>
                </Expander.ContentTemplate>
            </Expander>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Path=Packets}"
                 ItemTemplate="{StaticResource PacketItem}"
                 Grid.Row="1"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
    </Grid>

</UserControl>

代码

using System;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace wpfTester.UserControls
{
    public partial class test : UserControl
    {
        public ObservableCollection<clsPacket> Packets;

        public test()
        {
            InitializeComponent();
            Packets = new ObservableCollection<clsPacket>();
            PacketListBox.ItemsSource = Packets;

            for (int i = 0; i <= 100; i++)
            {
                Packets.Add(new clsPacket() { Offset = i, Hex = "00 11 22 33 44 55 66 77", Ascii = "ABCDEFGH" });
            }
        }
    }

    public class clsPacket
    {
        public long Offset { get; set; }
        public String Hex { get; set; }
        public String Ascii { get; set; }
    }
}

解决方案

经过一番挖掘后,我找到了解决方案here。问题是扩展器。我正在设置扩展器的标头/内容模板。但从未指定过自己的标题/内容。这是修改后的扩展器标签,用于解决我的问题。

<Expander IsExpanded="True" Header="{Binding}" Content="{Binding}">

2 个答案:

答案 0 :(得分:2)

您需要设置DataTemplates DataType(clsPacket)才能解析绑定。

我不确定其他DataTemplate是什么用,但这应该指向正确的方向让你的绑定正常工作。

<UserControl x:Class="WpfApplication11.test"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication11"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type local:clsPacket}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10*" />
                    <ColumnDefinition Width="65*" />
                    <ColumnDefinition Width="25*" />
                </Grid.ColumnDefinitions>

                <Grid Grid.Column="0">
                    <TextBox Text="{Binding Offset}"
                             FontFamily="Courier New"
                             Foreground="Blue"
                             Background="#FFCCCCCC"
                             TextWrapping="Wrap"
                             BorderThickness="0" />
                </Grid>

                <Grid Grid.Column="1">
                    <TextBox Text="{Binding Hex}"
                             FontFamily="Courier New"
                             TextWrapping="Wrap"
                             BorderThickness="0" />
                </Grid>

                <Grid Grid.Column="2">
                    <TextBox Text="{Binding Ascii}"
                             FontFamily="Courier New"
                             Background="#FFCCCCCC"
                             TextWrapping="Wrap"
                             BorderThickness="0" />
                </Grid>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Path=Packets}"
                 Grid.Row="1"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
    </Grid>

</UserControl>

代码:

namespace WpfApplication11
{
    public partial class test : UserControl
    {
        private ObservableCollection<clsPacket> _packets = new ObservableCollection<clsPacket>();

        public test()
        {
            InitializeComponent();
            DataContext = this;
            for (int i = 0; i <= 100; i++)
            {
                Packets.Add(new clsPacket() { Offset = i, Hex = "00 11 22 33 44 55 66 77", Ascii = "ABCDEFGH" });
            }
        }

        public ObservableCollection<clsPacket> Packets
        {
            get { return _packets; }
            set { _packets = value; }
        }
    }

    public class clsPacket
    {
        public long Offset { get; set; }
        public String Hex { get; set; }
        public String Ascii { get; set; }
    }
}

答案 1 :(得分:1)

经过一番挖掘后,我找到了解决方案here。问题是扩展器。我正在设置扩展器的标头/内容模板。但从未指定过自己的标题/内容。这是修改后的扩展器标签,用于解决我的问题。

<Expander IsExpanded="True" Header="{Binding}" Content="{Binding}">