WPF:所选项目的主/详细数据网格视图(内部列表或哈希表)
WPF : master / detail datagrid view from selected item (list or hashtable inside)
这是我第一次尝试使用 WPF,我只是尝试从主视图 (datagrid) 记录的选择中填充详细视图 (datagrid)。
我的代码几乎没有基于已发布的 CRUD 演示项目。
目前数据是为我的测试模拟生成的,重点是我的对象包含一个hastable 属性(如果更简单的话应该是一个列表)。
我设法从我的主视图到我的详细视图获取我选择的项目(即字符串...)的所有标准属性。
我的第二个数据网格不为空(我以前有一个列表而不是字典时就是这种情况)
我的第二个数据网格使用字典,但不使用列表
我无法直接在数据网格中编辑它的值:生成异常,是否有原因?
非常感谢
尼古拉斯
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>
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>
My Main Window
<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>
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
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" } });
}
}
这是我第一次尝试使用 WPF,我只是尝试从主视图 (datagrid) 记录的选择中填充详细视图 (datagrid)。 我的代码几乎没有基于已发布的 CRUD 演示项目。
目前数据是为我的测试模拟生成的,重点是我的对象包含一个hastable 属性(如果更简单的话应该是一个列表)。
我设法从我的主视图到我的详细视图获取我选择的项目(即字符串...)的所有标准属性。
我的第二个数据网格不为空(我以前有一个列表而不是字典时就是这种情况)
我的第二个数据网格使用字典,但不使用列表 我无法直接在数据网格中编辑它的值:生成异常,是否有原因?
非常感谢 尼古拉斯
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>
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>
My Main Window
<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>
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
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" } });
}
}