LTU
LTU

Reputation: 195

WPF. How to binding data from code to ListView

I have ListView which is write using Style and ControlTemplate, and I cannot make to show list which is in code-behind, maybe someone know what to do? Program is working, ListView Column Header is displaying, also scrollbar is, but no data listView item appear. What I write so far is:

xaml: All Style is written before ControlTemplate.

...<ControlTemplate x:Key="rounded" TargetType="ListView">
            <Border BorderBrush="Black" BorderThickness="0"  Height="490" Width="900" Margin="30,98,362,30"
                    HorizontalAlignment="Left" VerticalAlignment="Top" CornerRadius="8,8,0,0">
                <Grid>
                    <Border Name="myBorder" CornerRadius="8,8,0,0" Background="White" Margin="1"/>
                    <Grid>
                        <Grid.OpacityMask>
                            <VisualBrush Visual="{Binding ElementName=myBorder}"/>
                        </Grid.OpacityMask>
                        <ListView Style="{StaticResource ListView}" ScrollViewer.VerticalScrollBarVisibility="Visible"
                                  AlternationCount="2" ScrollViewer.CanContentScroll="False" SizeChanged="Size">
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn>
                                        <GridViewColumnHeader Content="Product" Margin="10,0,0,0" 
                                                              Style="{StaticResource GridViewColumnHeader}"/>
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <StackPanel Orientation="Vertical">
                                                    <Grid Height="42px" >
                                                        <TextBlock Text="{Binding odd}" VerticalAlignment="Center"  
                                                                   TextAlignment="Left" />
                                                    </Grid>
                                                    <TextBlock Text="{Binding discountText}" Padding="10,5,0,0" Style="{StaticResource MainText}"/>
                                                </StackPanel>
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

....

    <ListView Template="{StaticResource rounded}" 
ItemsSource="{Binding temp}" ItemContainerStyle="{StaticResource ListViewItem}">

.cs:

public class MyData
    {
        public string odd { get; set; }
        public double unitPrice { get; set; }
        public string quantity { get; set; }
        public double Price { get; set; }
        public bool IsDiscount { get; set; }
        public string discounText { get; set; }
        public double discountUnit { get; set; }
        public double discountPrice { get; set; }
    }


    public partial class MainWindow : Window
    {
    public List<MyData> temp
            {
                get; set; }


            public MainWindow()
            {
                InitializeComponent();

                List<MyData> sampleData = new List<MyData>

              {
                 new MyData{odd="Name 1", unitPrice=999999.99, quantity ="India1777. .hl978", Price=99999.99, 
                     IsDiscount=false, discounText="Surname 1", discountUnit=186, discountPrice=1.01},
                 new MyData{odd="Name 2", unitPrice=41.87, quantity ="India2", Price=47.56,
                     IsDiscount=false, discounText="Surname 2", discountUnit=84, discountPrice=12.45},
                 new MyData{odd="Name 3", unitPrice=234, quantity ="India3", Price=1.01,
                     IsDiscount=false, discounText="Surname 3", discountUnit=2744, discountPrice=45.84},
                 new MyData{odd="Name 4", unitPrice=2.45, quantity ="India4", Price=1.04,
                     IsDiscount=false, discounText="Surname 4", discountUnit=852, discountPrice=12},
     };
                temp = sampleData;

Upvotes: 0

Views: 288

Answers (2)

Aaron Thomas
Aaron Thomas

Reputation: 5281

When you implement InitializeComponent(), you are binding to a List<> that has not yet been populated.

Furthermore, when the List<> is populated, the properties of MyData currently do not raise an event... so nothing notifies the view that it needs to update.

To show changes to your List<> after InitializeComponent(), you'll need to make sure the properties in the List<> implement INotifyPropertyChanged.

See this MSDN link for more details.

Upvotes: 1

Valera Scherbakov
Valera Scherbakov

Reputation: 488

You can implement your sampleData as ObservableCollection:

ObservableCollection<MyData> temp = new ObservableCollection<MyData>();

and add items direct to temp property:

temp.Add(new MyData {...});

You can also inherit your MainWindow from INotifyPropertyChanged interface:

public partial class MainWindow : INotifyProppertyChanged
{
    public List<MyData> temp
    { 
        get 
        {
            return _temp;
        } 
        set
        {
            _temp = value;
            OnPropertyChanged();
        }
    }
    private List<MyData> _temp;

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

Upvotes: 0

Related Questions