【问题标题】:XAML Win8 Databinding TwoWay with an Observable CollectionXAML Win8 Databinding TwoWay 与 Observable 集合
【发布时间】:2014-05-30 13:27:43
【问题描述】:

我对 Win8 应用程序开发和 XAML 比较陌生(这是我的第一篇适当的堆栈溢出帖子,所以请轻描淡写!)。

在我的项目中,我正在创建一个订单页面,该页面显示订单标题详细信息,然后是订单行列表。 在订单行上,我需要用户能够更新数量(这是我正在努力解决的问题)。

我创建了以下类:

public class OrderDetails 
{

    public class OrderDetailsLine : INotifyPropertyChanged
    {
        public string ItemNo { get; set; }
        public string ItemDescription { get; set; }

        private decimal quantity;
        public decimal Quantity 
        { 
            get
            {
                return quantity;
            }

            set
            {
                quantity = value;
                OnPropertyChanged();
            }
        }


        public decimal Price { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string caller = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                   new PropertyChangedEventArgs(caller));
            }
        }
    }

    public class OrderDetailsHeader : INotifyPropertyChanged
    {
        public string OrderNo { get; set; }
        public string CustNo { get; set; }
        public string CustName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string PostCode { get; set; }

        private string _externalDocNo;
        public string ExternalDocNo 
        { 
            get
            {
                return _externalDocNo;
            }
            set
            {
                _externalDocNo = value;
                OnPropertyChanged();
            }
        }

        public DateTime PostingDate { get; set; }

        private ObservableCollection<OrderDetailsLine> _Lines = new ObservableCollection<OrderDetailsLine>();
        public ObservableCollection<OrderDetailsLine> Lines
        {
            get
            {
                return this._Lines;
            }

            set
            {
                _Lines = value;
                OnPropertyChanged();
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string caller = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                   new PropertyChangedEventArgs(caller));

            }
        }
    }

在我的 XAML 代码中,我有一个 TextBox,它使用两种方式对 OrderDetails.OrderDetailHeader.ExternalDocNo 进行数据绑定。

但是我也有一个带有 DataTemplate 的 ListView,其中一个 TextBox 绑定到 OrderDetails.OrderDetailHeader.Lines.Quantity

<ListView 
  HorizontalAlignment="Left"  
  Grid.Row="1" 
  VerticalAlignment="Top" 
  x:Name="ItemsListView"
  Margin="39,20,0,0"
  Width="Auto">
  <ListView.ItemTemplate>
      <DataTemplate>
          <Grid VerticalAlignment="Center" Width="800">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20*" />
                <ColumnDefinition Width="40*" />
                <ColumnDefinition Width="20*" />
                <ColumnDefinition Width="20*" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Margin="5" TextWrapping="NoWrap" 
                VerticalAlignment="Center"
                Text="{Binding ItemNo}" />

            <TextBlock Grid.Column="1" Margin="5" TextWrapping="Wrap"
                VerticalAlignment="Center"
                Text="{Binding ItemDescription}" />

            <TextBox Grid.Column="2" Margin="5" TextWrapping="NoWrap"
                HorizontalAlignment="Right"       
                VerticalAlignment="Center"
                Text="{Binding Quantity, Mode=TwoWay}" />

            <TextBlock Grid.Column="3" Margin="5" TextWrapping="NoWrap"
                HorizontalAlignment="Right"       
                VerticalAlignment="Center"
                Text="{Binding Price}" />


        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>

ItemsSource在

中设置如下
   private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
   {    
      ...
      this.ExternalDoc.DataContext = orderDetails.orderDetailsHeader;
      this.ItemsListView.ItemsSource = orderDetails.orderDetailsHeader.Lines;

当我在 TextBox 中更改 Quantity 的值时,它不会调用 PropertyChangedEventHandler - 我不知道为什么不这样做!

ExternalDoc 文本框在我更改时更新良好

<TextBox x:Name="ExternalDoc" HorizontalAlignment="Left" Grid.Row="1" TextWrapping="Wrap"  VerticalAlignment="Top" Height="27" Width="223" Margin="39,160,0,0" Text="{Binding ExternalDocNo, Mode=TwoWay}"/>

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 第一次发帖时+1
  • 如果您的 Quantity 或其他类似属性是类,那么您必须在这些类中实现 INotifyPropertyChanged。

标签: c# xaml windows-store-apps


【解决方案1】:

将 Text 绑定到小数属性时似乎存在问题。尝试将 Quantity 的类型更改为浮点数,看看是否可行。 更多内容:

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/b0bfe9b9-7634-40d2-8d81-c664498cae6a/winrt-xaml-binding-text-to-decimal-causes

【讨论】:

  • 很抱歉回复晚了,但是是的,这确实是问题!谢谢!
【解决方案2】:

我不能完全理解您的问题,但如果您仅更改列表中一行的数量属性,则不会调用 PropertyChangedEventHandler 是正常的。 您将 ObservableCollection 的实例绑定到 ListView 的 ItemSource 属性。如果您想提高 Lines 属性的 PropertyChangedEventHandler,您必须更改 ObservableCollection 的实例(在 setter 处访问)。 ObservableCollection 允许 WPF 在列表中“观察”修改。

我尝试了您的示例代码,但我不明白您如何获得 orderDetailsHeader,因为 orderDetail。我试试这段代码:

WpfApplication2.OrderDetails.OrderDetailsHeader header = new OrderDetails.OrderDetailsHeader(){
                Address = "tata",
                ExternalDocNo = "toto",
                Lines = new ObservableCollection<OrderDetails.OrderDetailsLine>()
            };
            header.Lines.Add(new OrderDetails.OrderDetailsLine()
            {
                ItemDescription = "toto",
                ItemNo = "titi",
                Price = 100,
                Quantity = 2
            });

            this.ExternalDoc.DataContext = header;
            this.ItemsListView.ItemsSource = header.Lines;

使用此代码和您的 xaml,当您更改数量和 ExteranlNoDoc 时会引发 NotifyPropertyChangedEvent。

【讨论】:

  • 感谢您的回复 - 是的,您正确理解了问题。不幸的是,我不太遵循您的解决方案-您能指出我正确的方向吗?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
相关资源
最近更新 更多