绑定WPF中的项目列表

时间:2017-03-09 13:47:03

标签: c# wpf mvvm binding

我尝试实现MVVM。 View中的TextBox文本被绑定到Model中的属性itemName。

在视图上是DataGrid - >绑定到ViewModel.Rows属性

在更改事件上的itemName上的ViewModel中,对产品的远程服务运行异步请求,这将转到模型SugestProducts属性。 SugestProducts属性是ListView项的源。

如果产品超过0列表视图打开。 ListView SelectedItem被绑定为模型产品属性。

我需要在列表视图中选择产品,从Product.name属性填充itemName属性,而不要求远程服务。其他工作很好。

我的模特是:

public class RowDocumentSaleWraper : INotifyPropertyChanged
{
    private ObservableCollection<Product> _sugestProducts;
    public ObservableCollection<Product> SugestProducts 
    { 
        get
        {
            return _sugestProducts;
        }
        set
        {
            _sugestProducts = value;
            NotifyPropertyChanged("SugestProducts");
        }
    }

        public Product product {get; set;}
         _itemName
        public override string itemName
        {
            get
            {
                return itemName;
            }
            set
            {
                itemName = value;
                NotifyPropertyChanged("itemName");
            }
        }  
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
}

产品:

public class Product
{
    public string name{get; set;}
}

我的ViewModel是:

public class OrderViewModel : DependencyObject
{
       public ObservableCollection<RowDocumentSaleWraper> Rows { get; set; }

    public OrderViewModel()
    {
        addNewRow();
    }
        internal void addNewRow()
        {
            RowDocumentSaleWraper row = new RowDocumentSaleWraper(Order);
            row.PropertyChanged += row_PropertyChanged;
            Rows.Add(row);
        }
    void row_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        RowDocumentSaleWraper row = sender as RowDocumentSaleWraper;

        if (row != null && e.PropertyName == "itemName" && !String.IsNullOrEmpty(row.itemName))
        {
            //get products from remote service -> source for 
            requestProducts(row.itemName, row);
        }
    }
    private async void requestProducts(string searchString, RowDocumentSaleWraper row)
    {
        if (!String.IsNullOrEmpty(searchString))
        {
            var products = await requestProductsAsync(searchString);

            row.SugestProducts = listToObservable(products);
        }

    }
}

我的Xaml:

<DataGrid Grid.Row="1" Name="mainDataGrid" ItemsSource="{Binding Rows , UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Product">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <TextBox PreviewKeyDown="TextBox_PreviewKeyDown" KeyDown="TextBox_KeyDown" Text="{Binding itemName, UpdateSourceTrigger=PropertyChanged}" MinWidth="200"/>
                                <ListView ItemsSource="{Binding SugestProducts, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
                                          KeyDown="ListView_KeyDown" SelectedItem ="{Binding product, UpdateSourceTrigger=PropertyChanged}">
                                    <ListView.View>
                                        <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
                                            <GridViewColumn  DisplayMemberBinding="{Binding code}"/>
                                            <GridViewColumn  DisplayMemberBinding="{Binding name}" />
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

1 个答案:

答案 0 :(得分:1)

根据以上评论,您应该通过itemName上的setter通知ViewModel

    public override string itemName
    {
        get
        {
            return itemName;
        }
        set
        {
            itemName = value;
            NotifyPropertyChanged("itemName");
            NotifyChange(itemName); 
        }
    } 

然后,您将定义事件以检索数据

private async void NotifyChange(string name)
{
    if (!String.IsNullOrEmpty(searchString))
    {
        var products = await requestProductsAsync(searchString);

        SugestProducts = listToObservable(products);
    }
}