There is an Observablecollection Fathers
that stores the ID-Firstname-Lastname father. How to make it so that when loading data from the child.lbs file, the ID
value that is written in the file under word[4]
is selected in the Combobox field? Item values are spelled out using XAML <Combobox "ItemsSource = {Binding Fathers}"/>
That is, so that when the file is loaded, the same values are restored as they were before the shutdown (the so-called Save/Load)
enter image description here enter image description here
Father.cs
namespace LabelBase.Models
{
public class Father : INotifyPropertyChanged
{
private int _id;
public int FatherID
{
get => _id;
set
{
_id = value;
OnPropertyChanged();
}
}
private string _firstname;
public string Firstname
{
get => _firstname;
set
{
_firstname = value;
OnPropertyChanged();
}
}
private string _secondname;
public string Secondname
{
get => _secondname;
set
{
_secondname = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
Child.cs
namespace LabelBase.Models
{
public class Child : INotifyPropertyChanged
{
private int _id;
public int ChildID
{
get => _id;
set
{
_id = value;
OnPropertyChanged();
}
}
private string _first;
public string Firstname
{
get => _first;
set
{
_first = value;
OnPropertyChanged();
}
}
private string _second;
public string Secondname
{
get => _second;
set
{
_second = value;
OnPropertyChanged();
}
}
private Father _father;
public Father Father
{
get => _father;
set
{
_father = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
child.lbs
contents:
13;Ken;Hollow;83
10;Fill;Kenory;93
father.lbs
contents:
83;Frank;Malkov
93;Jack;Miles
MainWindow.xaml:
<Grid>
<TabControl>
<TabItem Header="Save">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="Save" Margin="100" Height="50" Command="{Binding SaveButton}"/>
<Button Grid.Column="1" Content="Load" Margin="100" Height="50" Command="{Binding LoadButton}"/>
</Grid>
</TabItem>
<TabItem Header="Child">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Margin="5,5,5,50" Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Height="25" Margin="5" Text="{Binding ElementName=ChildGrid, Path=Columns[0].Header, StringFormat={}{0}: }" TextAlignment="Right"/>
<TextBlock Grid.Column="0" Grid.Row="1" Height="25" Margin="5" Text="{Binding ElementName=ChildGrid, Path=Columns[1].Header, StringFormat={}{0}: }" TextAlignment="Right"/>
<TextBlock Grid.Column="0" Grid.Row="2" Height="25" Margin="5" Text="{Binding ElementName=ChildGrid, Path=Columns[2].Header, StringFormat={}{0}: }" TextAlignment="Right"/>
<TextBlock Grid.Column="0" Grid.Row="3" Height="25" Margin="5" Text="{Binding ElementName=ChildGrid, Path=Columns[3].Header, StringFormat={}{0}: }" TextAlignment="Right"/>
<TextBox Grid.Column="1" Grid.Row="0" Height="25" Text="{Binding SelectedChildren.ChildID}" Margin="5"/>
<TextBox Grid.Column="1" Grid.Row="1" Height="25" Text="{Binding SelectedChildren.Firstname}" Margin="5"/>
<TextBox Grid.Column="1" Grid.Row="2" Height="25" Text="{Binding SelectedChildren.Secondname}" Margin="5"/>
<ComboBox Grid.Column="1" Grid.Row="3" Height="25" ItemsSource="{Binding Fathers}" SelectedItem="{Binding SelectedChildren.Father}" Margin="5">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Firstname"/>
<Binding Path="Secondname"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
<DataGrid Name="ChildGrid" Grid.Column="1" Margin="0,10,10,10" AutoGenerateColumns="False" ItemsSource="{Binding Children}" SelectedItem="{Binding SelectedChildren}">
<DataGrid.Columns>
<DataGridTextColumn Width="auto" Header="ID" Binding="{Binding ChildID}"/>
<DataGridTextColumn Width="auto" Header="Firstname" Binding="{Binding Firstname}"/>
<DataGridTextColumn Width="auto" Header="Secondname" Binding="{Binding Secondname}"/>
<DataGridTextColumn Width="auto" Header="Father" Binding="{Binding Father.FatherID}"/>
</DataGrid.Columns>
</DataGrid>
<Button Height="25" Margin="10,0,10,20" Content="Add Child" Command="{Binding AddChildButton}" VerticalAlignment="Bottom"/>
</Grid>
</TabItem>
<TabItem Header="Father">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Margin="5,5,5,50" Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Height="25" Margin="5" Text="{Binding ElementName=FatherGrid, Path=Columns[0].Header, StringFormat={}{0}: }" TextAlignment="Right"/>
<TextBlock Grid.Column="0" Grid.Row="1" Height="25" Margin="5" Text="{Binding ElementName=FatherGrid, Path=Columns[1].Header, StringFormat={}{0}: }" TextAlignment="Right"/>
<TextBlock Grid.Column="0" Grid.Row="2" Height="25" Margin="5" Text="{Binding ElementName=FatherGrid, Path=Columns[2].Header, StringFormat={}{0}: }" TextAlignment="Right"/>
<TextBox Grid.Column="1" Grid.Row="0" Height="25" Text="{Binding SelectedFathers.FatherID}" Margin="5"/>
<TextBox Grid.Column="1" Grid.Row="1" Height="25" Text="{Binding SelectedFathers.Firstname}" Margin="5"/>
<TextBox Grid.Column="1" Grid.Row="2" Height="25" Text="{Binding SelectedFathers.Secondname}" Margin="5"/>
</Grid>
<DataGrid Name="FatherGrid" Grid.Column="1" Margin="0,10,10,10" AutoGenerateColumns="False" ItemsSource="{Binding Fathers}" SelectedItem="{Binding SelectedFathers}">
<DataGrid.Columns>
<DataGridTextColumn Width="auto" Header="ID" Binding="{Binding FatherID}"/>
<DataGridTextColumn Width="auto" Header="Firstname" Binding="{Binding Firstname}"/>
<DataGridTextColumn Width="auto" Header="Secondname" Binding="{Binding Secondname}"/>
</DataGrid.Columns>
</DataGrid>
<Button Height="25" Margin="10,0,10,20" Content="Add Father" Command="{Binding AddFatherButton}" VerticalAlignment="Bottom"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
ContextVIew.cs:
using LabelBase.Models;
namespace LabelBase
{
public class ContextView : INotifyPropertyChanged
{
public ObservableCollection<Father> Fathers { get; }
public ObservableCollection<Child> Children { get; }
private Father _selectedFather;
public Father SelectedFathers
{
get => _selectedFather;
set
{
_selectedFather = value;
OnPropertyChanged();
}
}
private RelayCommand _addFatherButton;
public RelayCommand AddFatherButton
{
get
{
return _addFatherButton ?? (_addFatherButton = new RelayCommand(obj =>
{
Father f = new Father();
Random r = new Random();
f.FatherID = r.Next(99);
Fathers.Insert(Fathers.Count, f);
SelectedFathers = f;
}));
}
}
private Child _selectedChild;
public Child SelectedChildren
{
get => _selectedChild;
set
{
_selectedChild = value;
OnPropertyChanged();
}
}
private RelayCommand _addChildButton;
public RelayCommand AddChildButton
{
get
{
return _addChildButton ?? (_addChildButton = new RelayCommand(obj =>
{
Child c = new Child();
Random r = new Random();
c.ChildID = r.Next(99);
Children.Insert(Children.Count, c);
SelectedChildren = c;
}));
}
}
private RelayCommand _save;
public RelayCommand SaveButton
{
get
{
return _save ?? (_save = new RelayCommand(obj =>
{
StreamWriter swChild = new StreamWriter($"{AppDomain.CurrentDomain.BaseDirectory}/save/child.lbs", false);
StreamWriter swFather = new StreamWriter($"{AppDomain.CurrentDomain.BaseDirectory}/save/father.lbs", false);
foreach(Child item in Children)
{
string sandwich = $"{item.ChildID};{item.Firstname};{item.Secondname};{item.Father.FatherID}";
swChild.WriteLine(sandwich);
}
swChild.Close();
foreach (Father item in Fathers)
{
string sandwich = $"{item.FatherID};{item.Firstname};{item.Secondname}";
swFather.WriteLine(sandwich);
}
swFather.Close();
MessageBox.Show("Complete");
}));
}
}
private RelayCommand _load;
public RelayCommand LoadButton
{
get
{
return _load ?? (_load = new RelayCommand(obj =>
{
StreamReader srChild = new StreamReader($"{AppDomain.CurrentDomain.BaseDirectory}/save/child.lbs", false);
StreamReader srFather = new StreamReader($"{AppDomain.CurrentDomain.BaseDirectory}/save/father.lbs", false);
string lineChild, lineFather;
while((lineFather = srFather.ReadLine()) != null)
{
string[] word = lineFather.Split(';');
Father f = new Father();
f.FatherID = int.Parse(word[0]);
f.Firstname = word[1];
f.Secondname = word[2];
Fathers.Insert(Fathers.Count, f);
}
while((lineChild = srChild.ReadLine()) != null)
{
string[] word = lineChild.Split(';');
Child c = new Child();
c.ChildID = int.Parse(word[0]);
c.Firstname = word[1];
c.Secondname = word[2];
//c.Father.FatherID = int.Parse(word[3]);
Children.Insert(Children.Count, c);
}
srChild.Close();
MessageBox.Show("Complete");
}));
}
}
public ContextView(){
Fathers = new ObservableCollection<Father>();
Children = new ObservableCollection<Child>();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
There is a Project: https://github.com/RodKingroo/FatherChild
Who will help, thanks :)
CodePudding user response:
Thanks, @BionicCode 4 ur help
Answer ContextWiew.cs:
using (StreamReader srChild = new StreamReader($"{AppDomain.CurrentDomain.BaseDirectory}/save/child.lbs")) {
lineChild = srChild.ReadLine();
while ((lineChild = srChild.ReadLine()) != null)
{
string[] word = lineChild.Split(';');
Child c = new Child();
c.ChildID = int.Parse(word[0]);
c.Firstname = word[1];
p.Secondname = word[2];
foreach (Father item in Father)
{
if (item.FatherID == int.Parse(word[5])) p.Father = item;
}
Children.Add(c);
SelectedChildren = c;
}
srChild.Close();
}