以下是我的观点。第一个是主视图,第二个是另一个视图 它使用相同的视图模型和数据。它从excel获取数据并存储在网格中。问题是,在第一个主视图中,数据网格上的值正确显示,但是当我单击第一个视图上的可用按钮时,打开第二个视图,不显示记录但显示空白。我想在视图2上显示相同的记录。
在此处输入代码主视图: -
<Window x:Class="Main.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:PrestoControls="clr-namespace:PrestoControls;assembly=PrestoControls"
xmlns:local="clr-namespace:Main.ViewModel"
xmlns:localControls="clr-namespace:Main.View"
x:Name="Window"
Title="{Binding Scenario}"
Width="1400"
Height="800"
WindowStartupLocation="CenterScreen"
WindowState="Maximized">
<Grid>
<DataGrid x:Name="datagrid_sku_part_details"
Grid.Row="0"
Margin="10,5,0,5"
ItemsSource="{Binding SKUPartDetailsSubset,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedSku,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectionUnit="FullRow"
Style="{StaticResource style_main_datagrid}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="col_sku_part_details_class"
Width="60"
Binding="{Binding Class}"
Header="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource string_converter},
ConverterParameter='col_sku_part_details_class'}"
IsReadOnly="true" />
<DataGridTextColumn x:Name="col_sku_part_details_range"
Width="100"
Binding="{Binding Range}"
Header="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource string_converter},
ConverterParameter='col_sku_part_details_range'}"
IsReadOnly="true" />
<DataGrid.Columns>
</DataGrid>
<Button x:Name="btn_run_Demand_multipliers_in_budget_years1"
Margin="10,10,-9,10"
DockPanel.Dock="Bottom"
Style="{StaticResource style_btn_run_ranges}"
Content="Demand multipliers in budget years"
Command="{Binding DemandMultiplierInBudgetYear, Mode=TwoWay}" >
</Button>
<Grid>
第二视图
<Window x:Class="Main.View.MultiplierForBudgetReport"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MultiplierForBudgetReport" Height="500" Width="900"
xmlns:PrestoControls="clr-namespace:PrestoControls;assembly=PrestoControls"
xmlns:local="clr-namespace:Main.ViewModel"
xmlns:localControls="clr-namespace:Main.View"
xmlns:sync="clr-namespace:Main.ViewModel.Synchronisation"
x:Name="Window"
>
<Grid>
<Grid x:Name="grid_sku_part_details">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16*"/>
<ColumnDefinition Width="489*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<DataGrid x:Name="datagrid_sku_part_details"
Grid.Row="0"
Margin="10,5,0,5"
ItemsSource="{Binding SKUPartDetailsSubset,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedSku,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectionUnit="FullRow"
Style="{StaticResource style_main_datagrid}" Grid.ColumnSpan="2" SelectionChanged="datagrid_sku_part_details_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn x:Name="col_sku_part_details_class"
Width="60"
Binding="{Binding Class}"
Header="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource string_converter},
ConverterParameter='col_sku_part_details_class'}"
IsReadOnly="true" />
<DataGridTextColumn x:Name="col_sku_part_details_range"
Width="100"
Binding="{Binding Range}"
Header="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource string_converter},
ConverterParameter='col_sku_part_details_range'}"
IsReadOnly="true" />
<DataGridTextColumn x:Name="col_sku_part_details_sku_reference"
Width="150"
Binding="{Binding SKUReference}"
Header="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource string_converter},
ConverterParameter='col_sku_part_details_sku_reference'}"
IsReadOnly="true" />
<DataGridTextColumn x:Name="col_sku_part_details_part_reference"
Width="150"
Binding="{Binding PartReference}"
Header="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource string_converter},
ConverterParameter='col_sku_part_details_part_reference'}"
IsReadOnly="true" />
<DataGridTextColumn x:Name="col_sku_part_details_part_name"
Width="250"
Binding="{Binding PartName}"
Header="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource string_converter},
ConverterParameter='col_sku_part_details_part_name'}"
IsReadOnly="true" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid> enter code here
</Window>
下面是用于按钮的命令: -
public class DemandMultiplierBudYearCommand : ICommand
{
#region Fields
private MainWindowViewModel m_ViewModel;
#endregion
#region Constructor
public DemandMultiplierBudYearCommand(MainWindowViewModel viewModel)
{
m_ViewModel = viewModel;
}
#endregion
#region ICommand Members
/// <summary>
/// Whether the command is enabled.
/// </summary>
public bool CanExecute(object parameter)
{
return true;
}
/// <summary>
/// Actions to take when CanExecute() changes.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Executes the command
/// </summary>
public void Execute(object parameter)
{
View.MultiplierForBudgetReport Mfbg = new View.MultiplierForBudgetReport();
Mfbg.DataContext = m_ViewModel;
Mfbg.ShowDialog();
}
#endregion
}