簡體   English   中英

DataTemplate綁定不起作用?

[英]DataTemplate binding doesn't work?

我正在編寫以下代碼來綁定一些屬性

<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個項目,但是在運行app時我沒有看到任何添加的項目。 但當我用Width="{Binding Path=ChannelRecordTimeItemWidth}" Width="1000"替換Width="1000" Height="{Binding Path=ChannelRecordTimeItemHeight}"Height="20"替換Height="{Binding Path=ChannelRecordTimeItemHeight}" Height="20" ,它工作正常!

我想,這是綁定問題,但我不知道為什么。

有人可以告訴我如何使它工作?

非常感謝,

T&T

更新您的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 首先刪除Grid的綁定DataContext

    <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類。

由於這條線,您的解決方案無法正常工作

 DataContext="{Binding Path=ListRecordTime}" 

此行為網格設置datacontext,然后您嘗試從datacontext獲取ChannelRecordTimeItemHeight - 記錄時間列表。

刪除此行,看看會發生什么

暫無
暫無

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

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