2012-07-25 47 views
0

我有2个日期选择器的组合框上的视图模型我的主要XAML文件除了OnCurrentChanged使用什么,因为它不点火WPF MVVM

<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; 
     } 
    } 

} 

当程序第一次加载它进入成为当前更改并加载适当的日期。但是当我点击同一主xaml文件上的userControl中的按钮后,dateOptObj为空,当我尝试创建mdDateOptions的新实例时,它不会触发onCurrentChanged,因为没有任何更改!我的问题我怎么才能让我的dateOptObj包含正确的日期,当点击我的按钮在主要xaml文件上的userControl。提前致谢!

这是主要的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) 
    { 

    } 
} 

此代码的该用户控件

<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> 
+0

稍微非传统的绑定设置,但从我可以看到没有什么明显错误。你提到这个问题似乎是单击一个按钮导致你的datacontext的dateOptObj被清空。请发布XAML和任何代码隐藏或相关的命令,因为我不认为问题在于你发布的内容。 – 2012-07-25 22:36:09

+0

Steve我添加了XAML和后面的代码 – bewilderedprogrammer 2012-07-26 12:52:21

回答

0

的XAML的部分OnCurrentChanged正确着火,你的绑定是错误的。将DateTime属性绑定到SelectedDate而不是Text

<DatePicker Padding="5,5,5,5" 
      SelectedDate="{Binding Path=dateOptObj.openDate, Mode=TwoWay}" /> 
<DatePicker Padding="5,5,5,5" 
      SelectedDate="{Binding Path=dateOptObj.closeDate, Mode=TwoWay}" /> 
+0

我试过了Selecteddate我的DateOptObj仍然是null – bewilderedprogrammer 2012-07-26 12:51:58

+0

这可能不是发布代码的问题,因为我试过了你的代码,它工作到目前为止,除了绑定。 – LPL 2012-07-26 13:03:59

+0

是的,我真的不知道还有什么要尝试 – bewilderedprogrammer 2012-07-26 13:43:59

相关问题