【问题标题】:WPF. How to binding data from code to ListViewWPF。如何将数据从代码绑定到 ListView
【发布时间】:2015-11-03 17:52:57
【问题描述】:

我有使用 Style 和 ControlTemplate 编写的 ListView,但我无法显示代码隐藏中的列表,也许有人知道该怎么做? 程序正在运行,ListView Column Header 正在显示,滚动条也在显示,但没有数据 listView 项目出现。到目前为止我写的是:

xml: 所有样式都写在 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;

【问题讨论】:

    标签: wpf listview binding code-behind


    【解决方案1】:

    当您实现InitializeComponent() 时,您将绑定到尚未填充的List&lt;&gt;

    此外,当List&lt;&gt;被填充时,MyData 的属性当前不会引发事件......所以没有任何东西通知视图它需要更新。

    要在InitializeComponent() 之后显示对List&lt;&gt; 的更改,您需要确保List&lt;&gt; 中的属性实现INotifyPropertyChanged

    更多详情请见this MSDN link

    【讨论】:

    • 也许你可以展示如何将 INotifyPropertyChanged 导入我的代码,因为我得到了错误然后我尝试。太好了:)
    【解决方案2】:

    您可以将您的 sampleData 实现为 ObservableCollection:

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

    并将项目直接添加到临时属性:

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

    您还可以从 INotifyPropertyChanged 接口继承您的 MainWindow:

    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));
        }
    }
    

    【讨论】:

    • 也许你可以告诉我如何将“INotifyProppertyChanged”导入我的代码,因为如果我复制我得到错误,那就太好了:)
    • 你只需要在我的代码中添加你自己的 MainWindow() 构造函数:)
    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 2011-01-01
    • 2015-04-12
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多