WPF:所选项目的主/详细数据网格视图(列表或哈希表内)

时间:2015-05-06 12:46:17

标签: c# wpf datagrid master-detail

这是我第一次尝试使用WPF,我只是尝试从选择主视图(datagrid)记录中填充详细视图(datagrid)。 我几乎没有将我的代码基于已发布的CRUD演示项目。

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

我设法从我的主视图到我的详细视图获取所选项目的所有标准属性(即字符串...)。

我的第二个数据网格不是空的(当我以前有一个List而不是dictionnary时就是这种情况)

我的第二个数据网格使用字典,但它没有列表 我无法直接在数据网格中编辑其值:生成异常,是否有原因?

非常感谢 尼古拉斯

  

我的主视图

<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 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>

  

我的主窗口

<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>    

  

我的ConfigFile类

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

我的存储库

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

    }

}

1 个答案:

答案 0 :(得分:0)