0

我使用选项卡控件来查看我的用户控件。在第一个用户控件中,我使用数据网格来显示记录,而对于绑定,我使用了通用列表。当想要根据所选日期更改此列表时,该集合在数据库和视图模型中也随着列表的 set 属性执行而更改,但是当我选择新选项卡然后返回到 prevois 选项卡时,列表的 get 属性执行 &然后我可以根据选定的日期查看。

我的主视图包含第一个用户控件作为第一个选项卡项,如下所示:

在此处输入图像描述

上述视图的 Xaml 代码如下:

            <DataGrid
                Background="Transparent"
                CanUserAddRows="True"
                CanUserReorderColumns="False"

                ItemsSource="{Binding Model_Transactions_TransactionsDetails_Jama,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                HeadersVisibility="Column">
            </DataGrid>
            <Grid DockPanel.Dock="Bottom" VerticalAlignment="Bottom" >
                <Border BorderBrush="Black" BorderThickness="1" >
                    <Label HorizontalAlignment="Left" HorizontalContentAlignment="Right"  Width="75" Content="{Binding SumOfWeightJama,UpdateSourceTrigger=PropertyChanged}" FontFamily="Segoe UI" FontWeight="Bold"  FontSize="16" />
                </Border>
            </Grid>
            <DataGrid 
                Background="Transparent"
                CanUserAddRows="True"
                CanUserReorderColumns="False"
                ItemsSource="{Binding  Model_Transactions_TransactionsDetails_Udhar,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                HeadersVisibility="Column">
            </DataGrid>
            <Grid DockPanel.Dock="Bottom" VerticalAlignment="Bottom">
                <Border BorderBrush="Black" BorderThickness="1">
                    <Label Width="75" HorizontalAlignment="Left" HorizontalContentAlignment="Right"  Content="{Binding SumOfWeightUdhar,UpdateSourceTrigger=PropertyChanged}" FontFamily="Segoe UI" FontWeight="Bold"  FontSize="16"/>
                </Border>
            </Grid>

上述视图的视图模型如下:

private DateTime _FilterDate ;
public DateTime FilterDate
{
    get
    {
         return _FilterDate;
    }
    set
    {
        _FilterDate = value;
        OnPropertyChanged("FilterDate");
        Model_Transactions_TransactionsDetails_Jama = (ViewModel.AllDataCollactions.AllTransactionsDetails.Where(s => s.TransactionDate.Equals(FilterDate) && s.IsJama).OrderBy(s => s.TransactionsID)).ToList();
        Model_Transactions_TransactionsDetails_Udhar = (ViewModel.AllDataCollactions.AllTransactionsDetails.Where(s => s.TransactionDate.Equals(FilterDate) && !s.IsJama).OrderBy(s => s.TransactionsID)).ToList();

    }
}
public List<Model_TransactionsDetails> Model_Transactions_TransactionsDetails_Jama
{
    get
    {
        return model_Transactions_TransactionsDetails_Jama;
    }
    set
    {
        model_Transactions_TransactionsDetails_Jama = value;
        OnPropertyChanged("Model_Transactions_TransactionsDetails_Jama");
    }
}
public List<Model_TransactionsDetails> Model_Transactions_TransactionsDetails_Udhar
{
    get
    {
        return model_Transactions_TransactionsDetails_Udhar;
    }
     set
    {
        model_Transactions_TransactionsDetails_Udhar = value;
        OnPropertyChanged("Model_Transactions_TransactionsDetails_Udhar");
    }
}
public ViewModel_MasterBook()
{
    FilterDate = DateTime.Now.AddDays(-1).Date;
    InsertCommand = new RelayCommand(AddExecute, CanAdd);
}

任何人都可以帮助我如何立即根据所选日期获得视图..

4

1 回答 1

1

实际上它应该工作我看不到错误。但是当我在我的 WPF 项目中使用某种列表时,我使用带有清除、添加、删除功能的 observablecollection。

但首先我会改变绑定

ItemsSource="{Binding Model_Transactions_TransactionsDetails_Jama,Mode=OneWay}" 

因为 Mode=TwoWay 没有任何意义,所以您永远不会将数据网格中的 itemssource 设置为 viewmodel。

第二个我会改为 ObservableCollection

public ObservableCollection<Model_TransactionsDetails> Model_Transactions_TransactionsDetails_Jama
{
   get; private set;
}

使用私有设置器,因为只需初始化一次。

//ctor

this.Model_Transactions_TransactionsDetails_Jama = new ObservableCollection<Model_TransactionsDetails>();

然后在您的 FilterDate 设置器中填充集合

this.Model_Transactions_TransactionsDetails_Jama.Clear();
var newdata = (ViewModel.AllDataCollactions.AllTransactionsDetails.Where(s => s.TransactionDate.Equals(FilterDate) && s.IsJama).OrderBy(s => s.TransactionsID)).ToList();
this.Model_Transactions_TransactionsDetails_Jama.AddRange(newdata);//AddRange is simply an extension method i wrote, you can simply use foreach and .Add()
于 2012-09-14T09:00:13.437 回答