【发布时间】:2015-12-04 12:34:36
【问题描述】:
我需要在 W PF 的另一个窗口上的一个数据网格中显示两个不同来源的结果。或者可能我做错了。 所以我有这个模型
class StoreModel
{
public int ID { get; set; }
public string StrName { get; set; }
public TimeSpan TimeI { get; set; }
public TimeSpan TimeO { get; set; }
public string Actions { get; set; }//Added later to see if it works
}
还有这个观点
<DataGrid x:Name="strGrid" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Store" Binding="{Binding StrName}" Width="*"/>
<DataGridTextColumn Header="TimeIn" Binding="{Binding TimeI}" Width="*"/>
<DataGridTextColumn Header="TimeOut" Binding="{Binding TimeO}" Width="*"/>
<DataGridTemplateColumn Header="Action" Width="*" x:Name="comboTemp">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate x:Name="myTemplate">
<ComboBox x:Name="ActionCombo" IsReadOnly="True" IsEditable="True" Text="Select Action" SelectionChanged="ActionCombo_SelectionChanged">
<ComboBoxItem>Resolved</ComboBoxItem>
<ComboBoxItem>Issued</ComboBoxItem>
<ComboBoxItem>Pending</ComboBoxItem>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我的任务是从另一个页面的组合框中显示这些记录和选择的选项(来自 3、'Resolved'、'Issued'、'Pending')。 我不知道如何将组合框中的这两项包含在另一个页面的数据网格中。
我试图以自己的愚蠢方式来做,我在 selection_changed 事件的数组中检索了两个组合框的值(如果用户第三次尝试更改选择,这会导致另一个索引超出范围异常的问题,任何帮助这也是?)并试图将这两个源绑定在一起但没有工作。
public partial class StorePage : Window
{
List<StoreModel> MainstrModel;
string[] actions ;
static int selectedRow;
static int i = 0;
public StorePage()
{
InitializeComponent();
this.Loaded += StorePage_Loaded;
actions = new string[2];
MainstrModel = new List<StoreModel>();
}
void StorePage_Loaded(object sender, RoutedEventArgs e)
{
selectedRow = MainWindow.selectedRow;
MainstrModel.Add(new StoreModel() { ID = 1, StrName = "Store1", TimeI = DateTime.Now.TimeOfDay, TimeO = DateTime.Now.TimeOfDay, Actions = actions[i] });
MainstrModel.Add(new StoreModel() { ID = 1, StrName = "Store2", TimeI = DateTime.Now.TimeOfDay, TimeO = DateTime.Now.TimeOfDay, Actions = actions[i] });
MainstrModel.Add(new StoreModel() { ID = 2, StrName = "Store3", TimeI = DateTime.Now.TimeOfDay, TimeO = DateTime.Now.TimeOfDay, Actions = actions[i] });
MainstrModel.Add(new StoreModel() { ID = 3, StrName = "Store4", TimeI = DateTime.Now.TimeOfDay, TimeO = DateTime.Now.TimeOfDay, Actions = actions[i] });
strGrid.ItemsSource = MainstrModel.Where(x => x.ID == selectedRow);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
new MainWindow().Show();
this.Close();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
DetailPage detail = new DetailPage();
detail.detailGrid.ItemsSource = MainstrModel.Where(x => x.ID == selectedRow);
detail.Show();
this.Close();
}
private void ActionCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox combo = sender as ComboBox;
string item = combo.SelectedItem.ToString();
actions[i] = item.Substring(item.IndexOf(':') + 1);
i++;
}
}
我应该怎么做才能实现它?
【问题讨论】:
标签: c# wpf visual-studio xaml