WPF:所选项目的主/详细数据网格视图(列表或哈希表内) [英] WPF : master / detail datagrid view from selected item (list or hashtable inside)

查看:85
本文介绍了WPF:所选项目的主/详细数据网格视图(列表或哈希表内)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我第一次尝试使用WPF,我只是尝试从主视图的选择中填充详细视图(datagrid)( data.grid)记录。

我几乎没有将我的代码基于已发布的CRUD演示项目。



目前我的测试是模拟生成的数据,重要的一点是我的对象包含一个hastable属性(如果它更简单,应该是一个列表)。



我设法获得所选项目的所有标准属性(即字符串...)从我的主视图到我的详细视图,但是哈希表:第二个数据网格仍然是空的...有人可以帮助我吗?



非常感谢

Nicolas



>我的主视图



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

<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

<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





> My MainWindow

<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



> My ConfigFile class

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



> My repository

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" } });

     }

 }

推荐答案



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

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


The following link solved my problem :

http://www.broculos.net/2014/03/wpf-editable-datagrid-and.html#.VUoU-47tlBc
The following link solved my problem :
http://www.broculos.net/2014/03/wpf-editable-datagrid-and.html#.VUoU-47tlBc


这篇关于WPF:所选项目的主/详细数据网格视图(列表或哈希表内)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆