Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > WPF и Silverlight > Как осущесвить DataTemplate и DataBinding в коде


Автор: denimm 6.11.2010, 19:08


Код

ObservableCollection<Tournament> RRR  //Глобальная коллекция


при инициализации 

Код

private void Iniz()
        {
            
            DataTemplate cardLayout = new DataTemplate();
            cardLayout.DataType = typeof(Tournament);
            FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
            spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
            

            FrameworkElementFactory Picture = new FrameworkElementFactory(typeof(Image));
            Picture.SetBinding(Image.SourceProperty, new Binding("Picture"));
            Picture.SetValue(Image.MarginProperty, new Thickness(5));
            spFactory.AppendChild(Picture);


            FrameworkElementFactory TextBoxName = new FrameworkElementFactory(typeof(TextBlock));
            TextBoxName.SetBinding(TextBlock.TextProperty, new Binding("Name"));
            TextBoxName.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
            TextBoxName.SetValue(TextBlock.VerticalAlignmentProperty , VerticalAlignment.Center);
            TextBoxName.SetValue(Image.MarginProperty, new Thickness(5));           
            spFactory.AppendChild(TextBoxName);


            FrameworkElementFactory TextBoxRegTime = new FrameworkElementFactory(typeof(TextBlock));
            TextBoxRegTime.SetBinding(TextBlock.TextProperty, new Binding("RegTimeStr"));
            TextBoxRegTime.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
            TextBoxRegTime.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
            TextBoxRegTime.SetValue(Image.MarginProperty, new Thickness(5));           
            spFactory.AppendChild(TextBoxRegTime);


            FrameworkElementFactory TextTimeReg = new FrameworkElementFactory(typeof(TextBlock));
            TextTimeReg.SetBinding(TextBlock.TextProperty, new Binding("RegTimeLeftStr"));
            TextTimeReg.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
            TextTimeReg.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
            TextTimeReg.SetValue(Image.MarginProperty, new Thickness(5));
            spFactory.AppendChild(TextTimeReg);


            cardLayout.Resources.Add(spFactory, null);
            cardLayout.VisualTree = spFactory;
           
            

            Assembly asm = Assembly.GetExecutingAssembly();
            String[] gt = asm.GetManifestResourceNames();
            Stream PicStream = asm.GetManifestResourceStream(asm.GetName().Name + ".Resources.PicFTP.png");



            BitmapImage Pic = new BitmapImage();
            Pic.BeginInit();
            Pic.StreamSource = PicStream;
            Pic.EndInit();

            DataTemplate rt = new DataTemplate();               

            RRR = new ObservableCollection<Tournament>();            
          
            RRR.Add(new Tournament { Name = "Russia Freeroll", Room = PokerRoom.FTP, RegTime = new DateTime(2010, 11, 6, 12, 05, 54), StartTime = new DateTime(2010, 11, 3, 0, 55, 54), IsRegister = false, Picture = Pic });
            RRR.Add(new Tournament { Name = "Russia Freeroll", Room = PokerRoom.FTP, RegTime = new DateTime(2010, 11, 6, 13, 06, 54), StartTime = new DateTime(2010, 11, 3, 0, 55, 54), IsRegister = false, Picture = Pic });
            RRR.Add(new Tournament { Name = "Russia Freeroll", Room = PokerRoom.FTP, RegTime = new DateTime(2010, 11, 6, 14, 07, 54), StartTime = new DateTime(2010, 11, 3, 0, 55, 54), IsRegister = false, Picture = Pic });
            RRR.Add(new Tournament { Name = "Russia Freeroll", Room = PokerRoom.FTP, RegTime = new DateTime(2010, 11, 6, 15, 08, 54), StartTime = new DateTime(2010, 11, 3, 0, 55, 54), IsRegister = false, Picture = Pic });
            RRR.Add(new Tournament { Name = "Russia Freeroll", Room = PokerRoom.FTP, RegTime = new DateTime(2010, 11, 6, 16, 55, 54), StartTime = new DateTime(2010, 11, 3, 0, 55, 54), IsRegister = false, Picture = Pic });
            RRR.Add(new Tournament { Name = "Russia Freeroll", Room = PokerRoom.FTP, RegTime = new DateTime(2010, 11, 6, 17, 55, 54), StartTime = new DateTime(2010, 11, 3, 0, 55, 54), IsRegister = false, Picture = Pic });
            RRR.Add(new Tournament { Name = "Russia Freeroll", Room = PokerRoom.FTP, RegTime = new DateTime(2010, 11, 6, 19, 55, 54), StartTime = new DateTime(2010, 11, 3, 0, 55, 54), IsRegister = false, Picture = Pic });
           

            listBox1.ItemTemplate = cardLayout;
            listBox1.ItemsSource = RRR;
          





при обращении в коде

Код

RRR[0].RegTimeLeftStr = "10:21";
RRR

изменения не происходят


Можно делать так
Код

RRR[0].RegTimeLeftStr = "10:21";
listBox1.Items.Refresh();


но при вызове в таймере через какое-то время все жутко начинает тормозить



Как сделать так чтобы при изменении RRR  менялись значения в listBox1 ???




Автор: WolfTheGrey 9.11.2010, 12:47
Использовать INotifyPropertyChanged и в место ObservableColection использовать ListCollectionView

Автор: denimm 10.11.2010, 01:29
Спасибо, коллекцию не менял, помогло добавление  INotifyPropertyChanged

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)