我有一个更大的程序,但是现在我需要有一个DataGrid,它包含一些变量,最后一列必须是button,这将打开一个包含另一个datagrid的新窗口。问题是,我需要将带窗口的datagrid的所有行绑定到一个带按钮的datagrid中用户不可见列的行。 Button DataGrid中的一行是机械工的一天,不可见的列应保存他当天使用的所有材料。因此,无论如何,如何传递所有行(例如String变量)?
XAML(Windowed DataGrid):
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ServisZL_new"
xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" x:Class="ServisZL_new.MaterialAdd"
mc:Ignorable="d"
Title="Anderes Material" Height="1200" Width="1000" WindowState="Maximized" Loaded="Window_Loaded">
<ScrollViewer Background="#FFE5E5E5" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="0,0,0,-1" >
<Grid x:Name="MainMaterial" Height="1169">
<StackPanel>
<DataGrid x:Name="dg_material" Margin="10,10,10,0" HorizontalAlignment="Left" Height="142" Width="700" CanUserAddRows="True" AddingNewItem="DataGrid_AddingNewItem" CanUserDeleteRows="True" AutoGenerateColumns="False" PreviewKeyDown="dg_material_PreviewKeyDown">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Bezeichnung" x:Name="materialnazev">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Controls:AutoCompleteBox x:Name="ac_mat1" SelectedItem="{Binding Bezeichnung}" IsTextCompletionEnabled="True" FilterMode="Contains" MinimumPrefixLength="3" Loaded="ac_mat1_Loaded" SelectionChanged="ac_mat1_SelectionChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Menge" Binding="{Binding Menge}" />
<DataGridCheckBoxColumn Header="Box" Binding="{Binding Box}"/>
<DataGridTextColumn Header="SN" Binding="{Binding SN}" />
<DataGridTemplateColumn Header="Zylinder" x:Name="zylinder_name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cb_zyl01" SelectedItem="{Binding Zylinder}">
<ComboBoxItem Name="z00">--</ComboBoxItem>
<ComboBoxItem Name="z01">Zylinder 1</ComboBoxItem>
<ComboBoxItem Name="z02">Zylinder 2</ComboBoxItem>
<ComboBoxItem Name="z03">Zylinder 3</ComboBoxItem>
<ComboBoxItem Name="z04">Zylinder 4</ComboBoxItem>
<ComboBoxItem Name="z05">Zylinder 5</ComboBoxItem>
<ComboBoxItem Name="z06">Zylinder 6</ComboBoxItem>
<ComboBoxItem Name="z07">Zylinder 7</ComboBoxItem>
<ComboBoxItem Name="z08">Zylinder 8</ComboBoxItem>
<ComboBoxItem Name="z09">Zylinder 9</ComboBoxItem>
<ComboBoxItem Name="z10">Zylinder 10</ComboBoxItem>
<ComboBoxItem Name="z11">Zylinder 11</ComboBoxItem>
<ComboBoxItem Name="z12">Zylinder 12</ComboBoxItem>
<ComboBoxItem Name="z13">Zylinder 13</ComboBoxItem>
<ComboBoxItem Name="z14">Zylinder 14</ComboBoxItem>
<ComboBoxItem Name="z15">Zylinder 15</ComboBoxItem>
<ComboBoxItem Name="z16">Zylinder 16</ComboBoxItem>
<ComboBoxItem Name="z17">Zylinder 17</ComboBoxItem>
<ComboBoxItem Name="z18">Zylinder 18</ComboBoxItem>
<ComboBoxItem Name="z19">Zylinder 19</ComboBoxItem>
<ComboBoxItem Name="z20">Zylinder 20</ComboBoxItem>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="button" Content="Speichern un schließen" HorizontalAlignment="Left" Margin="10,23,0,0" VerticalAlignment="Top" Width="150" Click="button_Click"/>
</StackPanel>
</Grid>
</ScrollViewer>
XAML(按钮Datagrid):
<DataGrid x:Name="dg_work" HorizontalAlignment="Left" Height="142" Margin="11,396,0,0" VerticalAlignment="Top" Width="927" SelectionChanged="dg_work_SelectionChanged" AutoGenerateColumns="False" PreviewKeyDown="dg_work_PreviewKeyDown" AddingNewItem="dg_work_AddingNewItem" CanUserAddRows="True" CanUserDeleteRows="True" Grid.ColumnSpan="2">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Ident}" />
<DataGridTemplateColumn Header="Datum">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Datum}" BorderThickness="0" SelectedDateChanged="DatePicker_SelectedDateChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Arbaitszeit-Beginn" Binding="{Binding VonP}" />
<DataGridTextColumn Header="Arbeitszeit-Ende" Binding="{Binding BisP}" />
<DataGridTextColumn Header="Rüstzeit" Binding="{Binding Rzeit}" />
<DataGridTextColumn Header="Pause" Binding="{Binding Pzeit}" />
<DataGridTextColumn Header="Übernachtung" Binding="{Binding Ubern}" />
<DataGridTextColumn Header="Anfahrts-Zeit" Binding="{Binding ANZeit}" />
<DataGridTextColumn Header="Anfahrts-Km" Binding="{Binding ANkm}" />
<DataGridTextColumn Header="Rückfahrts-Zeit" Binding="{Binding RFzeit}" />
<DataGridTextColumn Header="Rückfahrts-Km" Binding="{Binding RFkm}" />
<DataGridTextColumn Header="Zusatzinfos" Binding="{Binding Zinfo}" />
<DataGridTemplateColumn Header="Material">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="button_add" Click="button_Click" Content="Material Anderse">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
背后的代码(Windowed DataGrid):
public partial class MaterialAdd : Window
{
public MaterialAdd()
{
InitializeComponent();
dg_material.ItemsSource = materialList;
{
private void button_Click(object sender, RoutedEventArgs e)
{
UlozMaterial();
predejtext();
this.Close();
}
List<Material> predej = new List<Material>();
public List<Material> materialList = new List<Material>();
private void DataGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
e.NewItem = new Material
{
Bezeichnung = "",
Menge = "",
Box = false,
SN = "",
Zylinder = ""
};
}
private void ac_mat1_Loaded(object sender, RoutedEventArgs e)
{
(sender as AutoCompleteBox).ItemsSource = GlobalZL.list_zbozi;
}
private void dg_material_PreviewKeyDown(object sender, KeyEventArgs e)
{
var uiElement = e.OriginalSource as UIElement;
if (e.Key == Key.Enter && uiElement != null)
{
e.Handled = true;
uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
private void ac_mat1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//text = (sender as AutoCompleteBox).SelectedItem.ToString();
}
public void predejtext()
{
string dg = dg_material.Items.ToString();
foreach (DataGridRow row in dg_material.Items)
{
System.Data.DataRow MyRow = (System.Data.DataRow)row.Item;
string value = MyRow["materialnazev"].ToString();
MessageBox.Show(value);
}
}
}
隐藏代码(按钮DataGrid):
private void dg_work_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
GlobalZL.dg_work_id += 1;
e.NewItem = new Item_work
{
Ident = GlobalZL.dg_work_id,
Datum = date,
VonP = "7:00",
BisP = "15:30",
Rzeit = "",
Pzeit = "",
Ubern = "",
ANZeit = "",
ANkm = "",
RFzeit = "",
RFkm = "",
Zinfo = "",
};
}
private void dg_work_PreviewKeyDown(object sender, KeyEventArgs e)
{
var uiElement = e.OriginalSource as UIElement;
if (e.Key == Key.Enter && uiElement != null)
{
e.Handled = true;
uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}