我仍然是WPF的新手,并试图掌握MVVM模式。我有mainWindow.xaml文件,有2个datepickers
<DatePicker Padding="5,5,5,5"
Text="{Binding Path=dateOptObj.openDate, Mode=TwoWay}"/>
<TextBlock Padding="5,5,5,5" Text=" to "/>
<DatePicker Padding="5,5,5,5"
Text="{Binding Path=dateOptObj.closeDate, Mode=TwoWay}"/>
当程序加载我的日期时,obj具有基于这些日期选择器的有效数据并加载匹配这两个日期的网格:
dateOptObj = new mdDateOptions();
现在这是加载该对象的实际类。
private ICollectionView _datOpts;
public ObservableCollection<string> dates { get; private set; }
public ICollectionView datesView
{
get
{
return _datOpts;
}
}
public mdDateOptions()
{
dates = new ObservableCollection<string>();
dates.Add("Rolling Year");
dates.Add("Year to Date");
dates.Add("Last Year");
_datOpts = CollectionViewSource.GetDefaultView(dates);
_datOpts.CurrentChanged += new EventHandler(OnCurrentChanged);
}
public void OnCurrentChanged(object sender, EventArgs e)
{
string currSel = (string)_datOpts.CurrentItem;
DateTime p_date = new DateTime();
p_date = DateTime.Today;
switch (currSel)
{
case "Rolling Year":
openDate = DateTime.Today.AddYears(-1);
closeDate = DateTime.Today;
openDatePrevious = DateTime.Today.AddYears(-2);
closeDatePrevious = DateTime.Today.AddYears(-1);
break;
case "Year to Date":
openDate = new DateTime(DateTime.Today.Year, 1, 1);
closeDate = DateTime.Today;
openDatePrevious = new DateTime(DateTime.Today.AddYears(-1).Year, 1, 1);
closeDatePrevious = DateTime.Today.AddYears(-1);
break;
case "Last Year":
openDate = new DateTime(DateTime.Today.AddYears(-1).Year, 1, 1);
closeDate = new DateTime(DateTime.Today.AddYears(-1).Year, 12, 31);
openDatePrevious = new DateTime(DateTime.Today.AddYears(-2).Year, 1, 1);
closeDatePrevious = new DateTime(DateTime.Today.AddYears(-2).Year, 12, 31);
break;
default:
MessageBox.Show("Unknown Date Option");
break;
}
}
我的问题是我在mainWindow.xaml上有一个userControl,它有一个按钮列表,用于在mainWindow.xaml上加载网格。当你点击userControl中的一个按钮时,我希望它根据日期选择器筛选出网格中的某些数据,但是当我的relay命令在viewmodel中触发时,上面的dateOptObj为null。谁能指出我正确的方向?以下是我加载UserControl的方法。
<vm:StatPanel DockPanel.Dock="Right" Loaded="StatPanel_Loaded" />
下面是我的UserControl面板,其中包含我想在MainWindow.xaml的主网格上过滤的按钮网格。
<DataGridTemplateColumn Header="Security">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="Security" Content="{Binding Path=totalSecurities}" Command="{Binding Source={StaticResource viewModel}, Path=filterGridCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource PassThroughConverter}">
<Binding Path="sector"/>
<Binding ElementName="Security" Path="Name"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
这是我的filterGridCommand
public RelayCommand filterGridCommand
{
get
{
if (_filterGridCommand == null)
{
_filterGridCommand = new RelayCommand(filterGrid);
}
return _filterGridCommand;
}
}
private void filterGrid(object param)
{
var values = (object[])param;
string sector = (string)values[0];
string type = (string)values[1];
EmployeeObj empObj = (EmployeeObj)_repsView.CurrentItem;
dash.reFillLogNewAccounts(dateOptObj.openDate, dateOptObj.closeDate, empObj.RRNumber,sector,type);
}