Kai Listner

Kai Listner

  • NA
  • 11
  • 669

Control a Datagrid from another class

May 8 2016 11:40 PM
Hello Community,
 
i have a problem with one part of my current project.
 
I have post it in this category because i Think it´s no problem from the WPF-Side...I have to be a problem with my C#-Code...But I didn´t know where is it... 
 
I have to control a Datagrid from another class
 
For better overview I have eliminated some of my code, but this is not important for the problem.
 
Here is my current Code from the Usercontrol. Here is my Datagrid inside:
  1. <UserControl x:Class="Lagerverwaltung_V3_0_Ribbon.MainFrame.TabItemArticle"  
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  6.              xmlns:local="clr-namespace:Lagerverwaltung_V3_0_Ribbon.MainFrame"  
  7.              xmlns:control = "clr-namespace:Lagerverwaltung_V3_0_Ribbon.Article">  
  8.     <StackPanel>  
  9.         <Button Height="20" Content="Test" Click="Button_Click"/>  
  10.         <DataGrid x:Name="dgArticle" AutoGenerateColumns="False" HeadersVisibility="None" IsReadOnly="True">  
  11.             <DataGrid.Columns>  
  12.                 <DataGridTemplateColumn Width="*">  
  13.                     <DataGridTemplateColumn.CellTemplate>  
  14.                         <DataTemplate>  
  15.                                             <!--Articlenumber-->  
  16.                                             <Label Name="ArticleID" Grid.Column="1" Content="{Binding ArticleID}" Margin="10,0,10,0" VerticalAlignment="Center"/>  
  17.                         </DataTemplate>  
  18.                     </DataGridTemplateColumn.CellTemplate>  
  19.                 </DataGridTemplateColumn>  
  20.             </DataGrid.Columns>  
  21.         </DataGrid>  
  22.     </StackPanel>  
  23. </UserControl>  
 and the C# Code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15.   
  16. namespace Lagerverwaltung_V3_0_Ribbon.MainFrame  
  17. {  
  18.     public partial class TabItemArticle : UserControl  
  19.     {  
  20.         public TabItemArticle()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.   
  25.         private void Button_Click(object sender, RoutedEventArgs e)  
  26.         {  
  27.             var data = new Test  
  28.             {  
  29.                 ArticleID = "#00010000",  
  30.             };  
  31.   
  32.   
  33.             dgArticle.Items.Add(data);  
  34.         }  
  35.         public class Test  
  36.         {  
  37.             public string ArticleID { getset; }  
  38.         }  
  39.     }  
  40. }  
 
And this is the Form where i want to Control it (my Main Window): 
  1. <Window x:Class="Lagerverwaltung_V3_0_Ribbon.MainWindow"      
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.         xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic"        
  5.         xmlns:TabItemGrid="clr-namespace:Lagerverwaltung_V3_0_Ribbon.MainFrame"              
  6.         xmlns:local="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"      
  7.         xmlns:system="clr-namespace:System;assembly=mscorlib"    
  8.         Title="Employee Info" Width="1400" Height="768">    
  9. <TabControl TabStripPlacement="Left" VerticalAlignment="Stretch" BorderBrush="#FF6593CF" BorderThickness="1,1,1,0" Style="{DynamicResource OutlookTabControlStyle}" SelectionChanged="TabControl_SelectionChanged">    
  10.                               <TabItem x:Name="MainWindow_SideControl_TabItem_Article" Header="Article">    
  11.                         
  12.                    <ScrollViewer VerticalScrollBarVisibility="Auto"> 


  13.                         <!-- Here the user control is imported -->   
  14.                         <TabItemGrid:TabItemArticle x:Name="tabItem_Article"/>    


  15.                     </ScrollViewer>    
  16.     
  17.                 </TabItem>    
  18.             </TabControl>   
  19.             <Button Height="20" Label="TestButton" Click="RibbonButton_Click" />
  20. </Window>    
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15. using System.Windows.Controls.Ribbon;  
  16. using System.Threading;  
  17.   
  18. namespace Lagerverwaltung_V3_0_Ribbon  
  19. {  
  20.     public partial class MainWindow : Window  
  21.     {  
  22.         public MainWindow()  
  23.         {  
  24.             InitializeComponent();  
  25.   
  26.         }  
  27.   
  28.         public class Test  
  29.         {  
  30.             public string ArticleID { getset; }  
  31.         }       
  32.           
  33.   
  34.         private void RibbonButton_Click(object sender, RoutedEventArgs e)  
  35.         {  
  36.             MainFrame.TabItemArticle TabArticle = new MainFrame.TabItemArticle();  
  37.             var data = new Test  
  38.             {  
  39.                 ArticleID = "#00010000",  
  40.             };  
  41.   
  42.             TabArticle.dgArticle.Items.Add(data);              
  43.         }  
  44.     }  
  45. }  
 My Problem is, that i can´t add a row to the datagrid with the button from the MainWindow.
Add a Row from the 'MainFrame.TabItemArticle' Control is no problem.
 
I hope you can understand my problem despite my bad english ...
 
Thank you for tips or other...
 
Greetings from Germany
 
Kai Listner 

Answers (3)