DataTemplate绑定不起作用?

时间:2013-12-11 05:53:12

标签: c# wpf binding

我正在编写以下代码来绑定一些属性

<StackPanel x:Name="channelsRecordTimeData" Orientation="Vertical">
    <ItemsControl x:Name="channelRecordTimeItems" ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate> 
            <DataTemplate>
                <Grid x:Name="gridChannelRecordTimeItem" Width="{Binding Path=ChannelRecordTimeItemWidth}"                                                                                                                
                      Height="{Binding Path=ChannelRecordTimeItemHeight}" Margin="{Binding Path=ChannelRecordTimeItemsMargin}"
                        HorizontalAlignment="Left" DataContext="{Binding Path=ListRecordTime}">
                    <Grid.Background>
                        <ImageBrush x:Name="gridChannelRecordTimeItemBgr" ImageSource="..\Resources\playback_grid_channel_record_time_item_bgr_normal.png"/>
                    </Grid.Background>                                    
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

public class DATA
{
    public double ChannelRecordTimeItemWidth { set; get; }
    public double ChannelRecordTimeItemHeight { set; get; }
    public Thickness ChannelRecordTimeItemsMargin { set; get; }
    public List<RecordTime> ListRecordTime { set; get; }

    public DATA()
    {
        ChannelRecordTimeItemWidth = 1000;
        ChannelRecordTimeItemHeight = 20;
        ChannelRecordTimeItemsMargin = new System.Windows.Thickness(0, 0, 0, 0);
        ListRecordTime = null;
    }
}

public static List<DATA> listDATA = new List<DATA>();
for(int i = 0 ; i < 10 ; i++)
{
    DATA data = new DATA();
    listDATA.Add(data);
}
channelRecordTimeItems.ItemsSource = listDATA;
channelRecordTimeItems.Items.Refresh();

在上面的代码中,我在StackPanel中添加了10个项目,但是在运行应用程序时我没有看到任何添加的项目。 但是当我将Width="{Binding Path=ChannelRecordTimeItemWidth}"替换为Width="1000"并将Height="{Binding Path=ChannelRecordTimeItemHeight}"替换为Height="20"时,它会正常工作!

我认为,这是绑定问题,但我不知道为什么。

有人可以告诉我如何让它发挥作用吗?

非常感谢,

T&amp; T公司

2 个答案:

答案 0 :(得分:1)

更新您的DATA课程以实施INotifyPropertyChanged,如下所示:

public class DATA : : INotifyPropertyChanged
{
    private double _channelRecordTimeItemWidth;
    private double _channelRecordTimeItemHeight;
    private Thickness _channelRecordTimeItemsMargin;
    private List<RecordTime> _listRecordTime;

    public double ChannelRecordTimeItemWidth 
    {
        get { return _channelRecordTimeItemWidth; }
        set
        {
            _channelRecordTimeItemWidth = value;
            OnPropertyChanged("ChannelRecordTimeItemWidth");
        }
    }

    public double ChannelRecordTimeItemHeight 
    {
        get { return _channelRecordTimeItemHeight; }
        set
        {
            _channelRecordTimeItemHeight = value;
            OnPropertyChanged("ChannelRecordTimeItemHeight");
        }
    }

    public Thickness ChannelRecordTimeItemsMargin 
    {
        get { return _channelRecordTimeItemsMargin; }
        set
        {
            _channelRecordTimeItemsMargin = value;
            OnPropertyChanged("ChannelRecordTimeItemsMargin");
        }
    }

    public List<RecordTime> ListRecordTime 
    {
        get { return _listRecordTime; }
        set
        {
            _listRecordTime = value;
            OnPropertyChanged("ListRecordTime");
        }
    }

    public DATA()
    {
        ChannelRecordTimeItemWidth = 1000;
        ChannelRecordTimeItemHeight = 20;
        ChannelRecordTimeItemsMargin = new System.Windows.Thickness(0, 0, 0, 0);
        ListRecordTime = null;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

这将通知XAML更新有界值。

DataContext也应正确设置。首先删除DataContext的绑定Grid

    <DataTemplate>
        <Grid x:Name="gridChannelRecordTimeItem" Width="{Binding Path=ChannelRecordTimeItemWidth}"                                                                                                                
              Height="{Binding Path=ChannelRecordTimeItemHeight}" Margin="{Binding Path=ChannelRecordTimeItemsMargin}"
                HorizontalAlignment="Left">
            <Grid.Background>
                <ImageBrush x:Name="gridChannelRecordTimeItemBgr" ImageSource="..\Resources\playback_grid_channel_record_time_item_bgr_normal.png"/>
            </Grid.Background>                                    
        </Grid>
    </DataTemplate>

并确保XAML的DataContext(无论是UserControl,Window等)是否设置为DATA类。

答案 1 :(得分:1)

由于此行

,您的解决方案无法正常工作
 DataContext="{Binding Path=ListRecordTime}" 

此行设置网格的datacontext,然后您尝试从datacontext获取ChannelRecordTimeItemHeight - 记录时间列表。

删除此行,看看会发生什么