我的主 xaml 文件上有一个带有 2 个日期选择器的组合框
<ComboBox Margin="5" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="90"
x:Name="datOpt"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=dateOptObj.dates}"
Background="{x:Null}" />
<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}" />
在视图模型中,我创建了一个这样的实例
public mdDateOptions dateOptObj { get; set; }
public vwMain()
{
try
{
// Initialize the panel switches
dateOptObj = new mdDateOptions();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
mdDateOptions 的类如下
class mdDateOptions : vwBase
{
private DateTime _openDate;
public DateTime openDate
{
get
{
return _openDate;
}
set
{
if (_openDate != value)
{
_openDate = value;
RaisePropertyChanged("openDate");
}
}
}
private DateTime _closeDate;
public DateTime closeDate
{
get
{
return _closeDate;
}
set
{
if (_closeDate != value)
{
_closeDate = value;
RaisePropertyChanged("closeDate");
}
}
}
private DateTime _openDatePrevious;
public DateTime openDatePrevious
{
get
{
return _openDatePrevious;
}
set
{
if (_openDatePrevious != value)
{
_openDatePrevious = value;
RaisePropertyChanged("openDatePrevious");
}
}
}
private DateTime _closeDatePrevious;
public DateTime closeDatePrevious
{
get
{
return _closeDatePrevious;
}
set
{
if (_closeDatePrevious != value)
{
_closeDatePrevious = value;
RaisePropertyChanged("closeDatePrevious");
}
}
}
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 mdDateOptions(Boolean value)
{
dates = new ObservableCollection<string>();
dates.Add("Rolling Year");
dates.Add("Year to Date");
dates.Add("Last Year");
_datOpts = CollectionViewSource.GetDefaultView(dates);
_datOpts.CollectionChanged += this.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;
}
}
}
当程序第一次加载时,它会进入 OncurrentChanged 并加载适当的日期。但是,当我在同一个主 xaml 文件上单击 userControl 中的按钮后,dateOptObj 为空,当我尝试创建 mdDateOptions 的新实例时,它不会触发 onCurrentChanged,因为没有任何改变!我的问题是如何在主 xaml 文件的 userControl 中单击我的按钮时让我的 dateOptObj 包含正确的日期。提前致谢!
这是主要的 Xaml 文件
<Window x:Class="ManagementDashboard.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ManagementDashboard"
Title="Dashboard Home" Height="768" Width="1024">
<Window.Resources>
<vm:vwMain x:Key="viewModel" />
</Window.Resources>
<DockPanel x:Name="viewModel"
DataContext="{Binding Source={StaticResource viewModel}}" >
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock Padding="5,5,5,5" Text="Date Options: "/>
<ComboBox Margin="5" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="90"
x:Name="datOpt"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=dateOptObj.dates}"
Background="{x:Null}" />
<DatePicker Padding="5,5,5,5"
SelectedDate="{Binding Path=dateOptObj.openDate, Mode=TwoWay}"/>
<TextBlock Padding="5,5,5,5" Text=" to "/>
<DatePicker Padding="5,5,5,5"
SelectedDate="{Binding Path=dateOptObj.closeDate, Mode=TwoWay}" />
<TextBlock Padding="5,5,5,5" Text="Rep Filter: "/>
<ComboBox Margin="5" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200"
x:Name="repOption"
IsSynchronizedWithCurrentItem="True"
DataContext="{Binding Source={StaticResource viewModel}}"
ItemsSource="{Binding Path=reps}"
DisplayMemberPath="AEname" />
<!--Background="{x:Null}" />-->
<Button Margin="5" Width="100" Height="20" Content="Reset Dates" Command="{Binding Path=Refill}" />
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom">
<Button Margin="5" Width="100" Height="20" Content="Exit" Command="{Binding Path=ExitCommand}" />
</StackPanel>
<vm:StatPanel DockPanel.Dock="Right" Loaded="StatPanel_Loaded" />
<vm:MainBody DataContext="{Binding Source={StaticResource viewModel}}"/>
</DockPanel>
这是后面的代码
public partial class MainWindow : Window
{
vwMain _viewModel;
public MainWindow()
{
InitializeComponent();
// Initialize the view Model
_viewModel = (vwMain)this.FindResource("viewModel");
}
private void StatPanel_Loaded(object sender, RoutedEventArgs e)
{
}
}
这是 UserControl 的 XAML 的一部分
<Expander Header="New Account Stats" IsExpanded="{Binding Path=newAcctPanel, Mode=TwoWay}" ExpandDirection="Down">
<DataGrid
x:Name="m_DataGrid"
ItemsSource="{Binding}"
DataContext="{Binding Path=dashNewAcct.newAcctStats}"
VerticalAlignment="Stretch" AutoGenerateColumns="False"
ColumnWidth="Auto">
<DataGrid.Columns>
<!--<DataGridTextColumn Header="Sector"
Binding="{Binding Path=sector}" />
<DataGridTextColumn Header="Security"
Binding="{Binding Path=totalSecurities}" />
<DataGridTextColumn Header="CD"
Binding="{Binding Path=totalCD}" />
<DataGridTextColumn Header="LPC"
Binding="{Binding Path=totalLPC}" />
<DataGridTextColumn Header="BSMS"
Binding="{Binding Path=totalBSMS, StringFormat='{}{0}'}" />
<DataGridTextColumn Header="Totals"
Binding="{Binding Path=total, StringFormat='{}{0}'}" />-->
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>Sector</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding Path=sector}"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<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>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>CD</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="CD" Content="{Binding Path=totalCD}" Command="{Binding Source={StaticResource viewModel}, Path=filterGridCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource PassThroughConverter}">
<Binding Path="sector"/>
<Binding ElementName="CD" Path="Name"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>LPC</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="LPC" Content="{Binding Path=totalLPC}" Command="{Binding Source={StaticResource viewModel}, Path=filterGridCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource PassThroughConverter}">
<Binding Path="sector"/>
<Binding ElementName="LPC" Path="Name" />
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>BSMS</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="BSMS" Content="{Binding Path=totalBSMS}" Command="{Binding Source={StaticResource viewModel}, Path=filterGridCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource PassThroughConverter}">
<Binding Path="sector"/>
<Binding ElementName="BSMS" Path="Name" />
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>Totals</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding Path=total}"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Expander>