メインの 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 が null になり、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>