我寫了下面的代碼綁定某些屬性DataTemplate綁定不起作用?
<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="1000"
代替Width="{Binding Path=ChannelRecordTimeItemWidth}"
並用Height="20"
代替Height="{Binding Path=ChannelRecordTimeItemHeight}"
時,那麼它工作的很好!
我想,這是綁定的問題,但我不知道爲什麼。
有人可以告訴我如何使它工作?
非常感謝,
牛逼&牛逼
這對解決我的問題,並展示了我如何使用INotifyPropertyChanged接口非常酷。非常感謝你! – TTGroup
我提出的另一個問題 http://stackoverflow.com/questions/20515233/how-to-notify-xaml-properties-when-list-data-of-binding-changed 你能看到它嗎?謝謝! – TTGroup