6 Replies - 731 Views - Last Post: 23 July 2019 - 09:09 AM Rate Topic: -----

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 309
  • Joined: 13-March 12

ListView of ViewModels Missing Data

Posted 10 July 2019 - 09:07 AM

I have a listview that is bound to a collection of ViewModels. A file arrives in a folder and it gets processed. First I see if it is already in the collection. If not I create a new object, set a couple of properties and add it to the collection. I use a DataType so that a UserControl is displayed for the ViewModel. When the processing completes, I can see the new item in the ListView, but the data is not visible. It is as though the object is a blank object rather than the one created.

Here is the "Process"
        public void ProcessArrivingTestResult(SampleReport sampleReport)
        {
            Statuswindow.StatusText = sampleReport.StatusText;
            Statuswindow.ProgressBarValue = sampleReport.Progress;
            FileOps.MoveFile(sampleReport.Sample);
            SampleViewModel svm;

            //If it is not in the collection then create it and add it to the collection.
            if (!Samples.Any(f => f.DisplayName == sampleReport.Sample.SampleID + "-" + sampleReport.Sample.TestID))
            {
                Sample samp = new Sample(sampleReport.Sample.SampleID, sampleReport.Sample.TestID);
                svm = new SampleViewModel(samp);
                Samples.Add(svm);
            }
            //get it from the collection and update the info.
            svm = Samples.First(x => x.DisplayName == sampleReport.Sample.SampleID + "-" + sampleReport.Sample.TestID);
            
            switch (sampleReport.Sample.CurrentFile)
            {
                case "Load":
                    svm.HasLoad = true;
                    break;
                case "Shrinkage":
                    svm.HasShrinkage = true;
                    break;
                case "Temp":
                    svm.HasTemp = true;
                    break;
            }

        }




SampleReport is a simple class to carry data from the background worker.

The datatype is defined in a resources file.
    <DataTemplate DataType="{x:Type vm:SampleViewModel}">
        <vw:SampleView />
    </DataTemplate>



And the SampleView looks like this.
    <UserControl.DataContext>
        <vm:SampleViewModel/>
    </UserControl.DataContext>

    <Border BorderBrush="Gray"
            BorderThickness="3"
            CornerRadius="3">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

            <DockPanel Grid.Column="0">
            <ProgressBar DockPanel.Dock="Bottom" 
                         Minimum="0"
                         Maximum="7"
                         Value="{Binding FullCount, Mode=OneWay}" 
                         RenderTransformOrigin="1,1"
                         IsIndeterminate="{Binding IsAvailable}"
                         Orientation="Vertical"
                         Width="30">
            </ProgressBar>
            </DockPanel>
        <Grid Grid.Column="1"
              Margin="5,0,0,0">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0">
                    <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="FontWeight" Value="Light"/>
                        <Setter Property="FontWeight" Value="Light"/>
                        <Setter Property="FontFamily" Value="Calibri" />
                        <Setter Property="FontSize" Value="14"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding HasLoad}" Value="True">
                                <Setter Property="FontWeight" Value="Bold"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
                <Run Text="{Binding SampleID, Mode=OneWay}"/>
                <Run Text="-"/>
                <Run Text="{Binding TestID, Mode=OneWay}"/>
                <Run Text="_Load Data"/>

            </TextBlock>
            <TextBlock Grid.Row="1">
                <Run Text="{Binding SampleID, Mode=OneWay}"/>
                <Run Text="-"/>
                <Run Text="{Binding TestID, Mode=OneWay}"/>
                <Run Text="_Shrink Data"/>
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="FontWeight" Value="Light"/>
                        <Setter Property="FontWeight" Value="Light"/>
                        <Setter Property="FontFamily" Value="Calibri" />
                        <Setter Property="FontSize" Value="14"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding HasShrinkage}" Value="True">
                                <Setter Property="FontWeight" Value="Bold"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
            <TextBlock Grid.Row="2">
                <Run Text="{Binding SampleID, Mode=OneWay}"/>
                <Run Text="-"/>
                <Run Text="{Binding TestID, Mode=OneWay}"/>
                <Run Text="_Temp Data"/>
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="FontWeight" Value="Light"/>
                        <Setter Property="FontWeight" Value="Light"/>
                        <Setter Property="FontFamily" Value="Calibri" />
                        <Setter Property="FontSize" Value="14"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding HasTemp}" Value="True">
                                <Setter Property="FontWeight" Value="Bold"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>            
        </Grid>
        </Grid>
    </Border>



The MainWindow has this ListView defined.
            <ListView ItemsSource="{Binding Samples}"
                      SelectedItem="{Binding SampleSelected}"
                      ScrollViewer.VerticalScrollBarVisibility="Visible"
                      IsSynchronizedWithCurrentItem="True"
                      SelectionMode="Single"
                  />



Samples is an observableCollection of SampleViewModel.

When I run it I end up with a SampleView in the list box without the data it should have. Tracing through the process I can see the data is present. I am missing something. Maybe you can see it.

Thanks for looking,

Is This A Good Question/Topic? 0
  • +

Replies To: ListView of ViewModels Missing Data

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: ListView of ViewModels Missing Data

Posted 10 July 2019 - 09:51 AM

Can you show us the SampleViewModel class?
Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 634
  • View blog
  • Posts: 4,201
  • Joined: 12-January 10

Re: ListView of ViewModels Missing Data

Posted 10 July 2019 - 09:58 AM

Im not sure what information you are looking for because you state that it shows in the drop down but no other info is shown. where is this info supposed to show? if you mean strictly in the drop down-- you are only adding the id's to that drop down
Was This Post Helpful? 0
  • +
  • -

#4 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 309
  • Joined: 13-March 12

Re: ListView of ViewModels Missing Data

Posted 10 July 2019 - 10:08 AM

Thanks for your reply Skydiver. Here is the SampleViewModel

    public class SampleViewModel : ViewModelBase
    {
        public ObservableCollection<string> files = new ObservableCollection<string>();
        private Sample sample;
        private string sampleID;
        private string testID;
        private bool hasLoad;
        private bool hasShrinkage;
        private bool hasTemp;
        private int fullCount;
        private bool isAvailable = false;

        public bool HasLoad
        {
            get { return hasLoad; }
            set
            {
                hasLoad = value;
                if (value == true)
                {
                    int count = fullCount;
                    int newcount = 1;
                    fullCount = count | newcount;
                }
                else
                {
                    int count = fullCount;
                    int newcount = 6;
                    fullCount = count & newcount;
                }
                CheckCount();
                RaisePropertyChanged();
            }
        }

        public Sample ThisSample
        {
            get { return sample; }
            set
            {
                sample = value;
                RaisePropertyChanged();
            }
        }

        public bool HasShrinkage
        {
            get { return hasShrinkage; }
            set
            {
                hasShrinkage = value;
                if (value == true)
                {
                    int count = fullCount;
                    int newcount = 2;
                    fullCount = count | newcount;
                }
                else
                {
                    int count = fullCount;
                    int newcount = 5;
                    fullCount = count & newcount;
                }
                CheckCount();
                RaisePropertyChanged();
            }
        }

        public bool HasTemp
        {
            get { return hasTemp; }
            set
            {
                hasTemp = value;
                if (value == true)
                {
                    int count = fullCount;
                    int newcount = 4;
                    fullCount = count | newcount;
                }
                else
                {
                    int count = fullCount;
                    int newcount = 3;
                    fullCount = count & newcount;
                }
                CheckCount();
                RaisePropertyChanged();
            }
        }

        public bool IsAvailable
        {
            get { return isAvailable; }
            set
            {
                isAvailable = value;
                RaisePropertyChanged();
            }
        }

        public int FullCount
        {
            get { return fullCount; }
        }

        public void CheckCount()
        {
            if (fullCount == 7)
                IsAvailable = true;
        }

        public string SampleID
        {
            get { return sampleID; }
            set
            {
                sampleID = value;
                RaisePropertyChanged();
            }
        }

        public string TestID
        {
            get { return testID; }
            set
            {
                testID = value;
                RaisePropertyChanged();
            }
        }

        public SampleViewModel(Sample samp)
        {
            ThisSample = samp;
            SampleID = samp.SampleID;
            TestID = samp.TestID;
            DisplayName = SampleID + "-" + TestID;
            fullCount = 0;
        }

        public SampleViewModel()
        {
        }
    }




DarenR, I don't know what Drop Down you mean?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: ListView of ViewModels Missing Data

Posted 10 July 2019 - 04:13 PM

I'm sorry, I'm not thinking clearly enough right now. My gut feel is that the <UserContext.DataContext> element maybe fighting with the <DataTemplate> but I'm not sure.
Was This Post Helpful? 0
  • +
  • -

#6 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 309
  • Joined: 13-March 12

Re: ListView of ViewModels Missing Data

Posted 23 July 2019 - 08:40 AM

Well Skydiver, Not thinking clearly did not prevent you from having the right answer. NOW, however, I could use another one of your not thinking clearly answers. I have the samples showing in the listview and I am able to modify the properties of each. But the SelectedItem does not work. When I select it, the list highlights to show the section, but it doesn't fall to SampleSelected.

      XAML
            <ListView ItemsSource="{Binding Samples}"
                      SelectedItem="{Binding SampleSelected}"
                      ScrollViewer.VerticalScrollBarVisibility="Auto"
                      IsSynchronizedWithCurrentItem="True"
                      SelectionMode="Single"/>

     MainWindowViewModel

        public Sample SampleSelected
        {
            get
            { return _selectedSample; }
            set
            {
                if (value == null) return;
                _selectedSample = value;
                _selectedSample.IsSelected = true;
                TestResult tr = Tests.First(f => f.SampleID + "-" + f.TestID == value.SampleID + "-" + f.TestID);
                if (!Results.Any(p => p.DisplayName == _selectedSample.SampleID + "-" + _selectedSample.TestID))
                    DisplayTestResult(tr);
            }
        }




Thanks for the assist!
Was This Post Helpful? 0
  • +
  • -

#7 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 309
  • Joined: 13-March 12

Re: ListView of ViewModels Missing Data

Posted 23 July 2019 - 09:09 AM

Nevermind. I did not change the return type of the SampleSelected to SampleViewModel. When I did everything worked fine.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1