Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

This my very first attempt to work with WPF, I just try to populate a detail view (datagrid) from the selection of a master view (datagrid) record.
I hardly based my code on a published CRUD demo project.

Currently the data are mock generated for my test, the important point is that my object contains a hastable property (should be a list if it's simpler).

I manage to get all standard properties of my selected item (ie string ...) from my master view to my detail view, but with the hashtable : the second datagrid remains empty... could someone helps me ?

Many thanks
Nicolas

> My Master View

XML
<UserControl x:Class="SymoConfigCrousLector.View.ListeMachine"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Height="Auto" Width="Auto">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <Label Content="Search :" Margin="0,4,0,-4"/>
            <TextBox Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="5">
                <TextBox.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SearchContainsNoMatch}" Value="True">
                                <DataTrigger.Setters>
                                    <Setter Property="TextBox.Background" Value="#68FF0000"/>
                                    <Setter Property="TextBox.ToolTip" Value="No result found"/>
                                </DataTrigger.Setters>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>
        </DockPanel>


        <DataGrid ItemsSource="{Binding ConfigFiles}"  IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" Grid.Row="1"  HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top" Height="Auto" Width="Auto">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Serial number" Binding="{Binding SerialNumber}" />
            </DataGrid.Columns>
        </DataGrid>

        <UniformGrid Grid.Row="2" Columns="2">
            <Button Content="Add" Command="{Binding AddCommand}"/>
            <Button Content="Delete" Command="{Binding RemoveCommand}"/>
            
        </UniformGrid>
        
    </Grid>
</UserControl>


> My Detail view
XML
<UserControl x:Class="SymoConfigCrousLector.View.Detail"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="600">
    <Grid Margin="0,0,0,0" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            
        </Grid.ColumnDefinitions>
        
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="15*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Label Content="cnous.cfg" Grid.Row="0" Grid.Column="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
        <Label Content="network.cfg" Grid.Row="0" Grid.Column="1" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
        <Label Content="pricelist.cfg" Grid.Row="0" Grid.Column="2" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>

        <!--my datagrid remains empty-->
        <DataGrid ItemsSource="{Binding Path=SelectedFile.conf}" AutoGenerateColumns="false" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top" Height="Auto" Width="Auto">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Param list" />
            </DataGrid.Columns>
        </DataGrid>

        <!--this works : get the correct Filename property-->
        <TextBox DataContext="{Binding SelectedFile}" Grid.Column="0" Grid.Row="2" Text="{Binding Path=FileName, Mode=TwoWay}"/>
        
    </Grid>
</UserControl>


> My MainWindow

XML
<Window x:Class="SymoConfigCrousLector.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:View="clr-namespace:SymoConfigCrousLector.View"
        Title="My test application" Height="531.915" Width="754.787">
    <Grid x:Name="MainGrid" Height="505.106" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <!--Bind l'une des vues sur le container principal-->
        <View:ListeMachine x:Name="ListeMachine" Grid.Column="0"/>
        <View:Detail x:Name="Detail" Grid.Column="1"/>
        
    </Grid>    
</Window>


> My ConfigFile class
C#
public class ConfigFile
{
    public string FileURL { get; set; }
    public string FileName { get; set; }
    public string SerialNumber { get; set; }
    public Dictionary<string, string> conf { get; set; }
}


> My repository
C#
class ConfigFileRepository
 {
     public List<ConfigFile> ListConfigFiles { get; private set; }
     public string path {get; set;}

     public ConfigFileRepository(string path)
     {
         this.path = path;
         ListConfigFiles = new List<ConfigFile>();
     }

     public ConfigFile Add(string FileURL, string FileName, string SerialNumber, Dictionary<string, string> conf)
     {
         ConfigFile myFile = new ConfigFile { FileURL = FileURL, FileName = FileName, SerialNumber = SerialNumber, conf = conf };

         this.ListConfigFiles.Add(myFile);
         return myFile;
     }

     public void Remove(ConfigFile myConfigFile)
     {
         this.ListConfigFiles.Remove(myConfigFile);
     }

     public List<ConfigFile> GetAll()
     {
         return ListConfigFiles;
     }

     public void CreateMockData()
     {
         this.Add("C:/test/data", "File 1", "FIC1", new Dictionary<string, string> { { "Param1", "Value1" }, { "Param2", "Value2" } });
         this.Add("C:/test/data", "File 2", "FIC2", new Dictionary<string, string> { { "Param1", "Value1" }, { "Param2", "Value2" } });
         this.Add("C:/test/data", "File 3", "FIC3", new Dictionary<string, string> { { "Param1", "Value1" }, { "Param2", "Value2" } });
         this.Add("C:/test/data", "File 4", "FIC4", new Dictionary<string, string> { { "Param1", "Value1" }, { "Param2", "Value2" } });
         this.Add("C:/test/data", "File 5", "FIC5", new Dictionary<string, string> { { "Param1", "Value1" }, { "Param2", "Value2" } });
         this.Add("C:/test/data", "File 6", "FIC6", new Dictionary<string, string> { { "Param1", "Value1" }, { "Param2", "Value2" } });

     }

 }
Posted

[EDIT]
My second datagrid is not empty anymore (it was the case when I used to have a List<string> instead of dictionnary)

My second DataGrid now works with a dictionary.
But I'm not able to edit its values : exception generated, is there a reason ?

Thanks
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900