Пишу окно истории проектов (что-то типа Visual Studio). Приведённый здесь код, при добавлении каждого нового пункта списка, правит данные сразу у всех пунктов, делая их одинаковыми. Никак не могу исправить данный код, чтобы при добавлении нового пункта, он имел свои данные (отличные от других пунктов). Я ещё не волшебник, а только учусь, поэтому прошу помощи у сообщества!
XAML-код:
// шаблон пункта списка
<ListBox x:Name="listBox" Grid.Row="1" Grid.Column="0" BorderThickness="1" Margin="30,0,0,0"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionMode="Single">
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<!-- строку полного пути в 'Tag' нового пункта -->
<Setter Property="Tag" Value="{Binding fullFileNameProject, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/>
<!-- Содержимое пункта (иконка и две строки) -->
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="26"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- иконка логотип -->
<Image x:Name="logoName" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Width="24" Height="24" Stretch="None" VerticalAlignment="Top"
Margin="0,9,0,0" Source="{Binding logoProject, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/>
<!-- имя файла -->
<TextBlock x:Name="textBlock1" Grid.Column="1" Grid.Row="0" FontSize="13" FontWeight="Bold"
Margin="2,3,0,0" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Left"
Text="{Binding fileProject, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/>
<!-- строка пути папки -->
<TextBlock x:Name="textBlock2" Grid.Column="1" Grid.Row="1" FontSize="12"
Margin="2,0,0,9" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Left"
Text="{Binding folderProject, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
C#-код:
// иконка пункта
private string _logoProj;
public string logoProject {
private set {
_logoProj = value;
OnPropertyChanged("logoProject");
}
get { return _logoProj; }
}
// имя файла
private string _fileProj;
public string fileProject {
private set {
_fileProj = value;
OnPropertyChanged("fileProject");
}
get { return _fileProj; }
}
// путь папки
private string _folderProj;
public string folderProject {
private set {
_folderProj = value;
OnPropertyChanged("folderProject");
}
get { return _folderProj; }
}
// строка "путь папки + имя файла"
private string _fullfnameProj;
public string fullFileNameProject {
private set {
_fullfnameProj = value;
OnPropertyChanged("fullFileNameProject");
}
get { return _fullfnameProj; }
}
// INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
C#-код (добавить новый пункт):
// счётчик
public int cnt = 0;
// Добавить новый пункт в список
// иконка пункта
logoProject = @"/Resource/iar24.png";
// строка имени файла
fileProject = "ide" + String.Format("{0}.iprj", ++cnt);
// строка папки
folderProject = @"C:\Microsoft Visual Studio\Projects_wpf\ide" + String.Format("{0}", cnt);
// строка полного пути файла
fullFileNameProject = System.IO.Path.Combine(folderProject, fileProject);
// создать пункт
var item = new ListBoxItem();
// добавить пункт в список
listBox.Items.Add(item);