I ran into a strange error
This is the control
<UserControl x:Class="PHOWjection_Software_3._0.CustomControls.LyricsDisplay"
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"
xmlns:local="clr-namespace:PHOWjection_Software_3._0.CustomControls"
mc:Ignorable="d" Background="White"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ListView x:Name="View" Grid.Row="1" Foreground="Black" ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderThickness="0" ScrollViewer.CanContentScroll="True">
<ListView.View>
<GridView x:Name="ViewGridView">
<GridViewColumn Header="HI" Width="30">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Background="{Binding HeaderBGBrush}">
<!--<Rectangle Fill="{Binding HeaderBrush}" />-->
<TextBlock Text="{Binding HeaderIcon}" Foreground="{Binding HeaderFGBrush}" HorizontalAlignment="Center" Tag="{Binding Identity}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Abbreviation" Width="500">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Block_OneLine}" Tag="{Binding Identity}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
</UserControl>
Here is the code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PHOWjection_Software_3._0.ItemType;
namespace PHOWjection_Software_3._0.CustomControls
{
public partial class LyricsDisplay : UserControl
{
public LyricsDisplay()
{
InitializeComponent();
}
public void Display(Song Song)
{
View.Items.Clear();
foreach (Lyrics_Block LB in Song.Lyrics_Blocks)
{
View.Items.Add(LB);
}
//Song.Lyrics_Blocks.ForEach(x => View.Items.Add(x));
}
}
}
Here is the Code for Song and Lyrics_Block (the class being added to the listview)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Threading;
using System.Globalization;
namespace PHOWjection_Software_3._0.ItemType
{
public partial class Song : DataBaseItem
{
public Song Identity { get { return this; } }
public Song()
{
}
private string _Title = ""; public string Title { get => _Title; set { _Title = value; PropertyChanged(DBProperties.Title); } }
private string _Author = ""; public string Author { get => _Author; set { _Author = value; PropertyChanged(DBProperties.Author); } }
private string _Lyrics = ""; public string Lyrics { get => _Lyrics; set { _Lyrics = value; PropertyChanged(DBProperties.Lyrics); Set_Lyrics_Block(); } }
private bool _IsFavorite = false; public bool IsFavorite { get => _IsFavorite; set { _IsFavorite = value; PropertyChanged(DBProperties.IsFavorite); } }
public void Set_Lyrics_Block()
{
string MaxWidth = ""; Max_Lyrics_Block = new Lyrics_Block();
Lyrics_Blocks = new List<Lyrics_Block>();
List<string> List = new List<string>(_Lyrics.Split(new string[] { "\r\n" }, StringSplitOptions.None));
Lyrics_Block LB = new Lyrics_Block();
foreach (string Line in List)
{
if (Line.Length > MaxWidth.Length) MaxWidth = Line;
bool ForceContinue = false;
foreach (Header Header in MainLogic.HeaderList.Values)
{
string HeadLow = Header.Title.ToLower();
string LinLow = Line.ToLower();
if (LinLow.IndexOf(HeadLow) == 0)
{
if (LB.Header != null) { Lyrics_Blocks.Add(LB); LB = new Lyrics_Block(); }
LB.Header = Header;
LB.HeaderFGBrush = Header.ForeGround_Brush;// The issue is here
LB.HeaderBGBrush = Header.BackGround_Brush;// The issue is here
LB.HeaderLine = Line;
LB.Set_Header_Icon();
ForceContinue = true;
}
}
if (ForceContinue) continue;
if (Line == "") { Lyrics_Blocks.Add(LB); LB = new Lyrics_Block(); }
else
{
LB.Block_OneLine += Line + " ";
LB.Block += Line + Environment.NewLine;
LB.LineCount++;
}
}
Max_Lyrics_Block.LineCount = Lyrics_Blocks.Select(xc => xc.LineCount).ToList().Max();
for (int x = 0; x < Max_Lyrics_Block.LineCount; x++)
Max_Lyrics_Block.Block += MaxWidth + Environment.NewLine;
}
public List<Lyrics_Block> Lyrics_Blocks = new List<Lyrics_Block>();
public Lyrics_Block Max_Lyrics_Block = new Lyrics_Block();
}
public partial class Lyrics_Block
{
public Lyrics_Block Identity { get { return this; } }
private Header _Header { get; set; }
public Header Header { get { return _Header; } set { _Header = value; } }
public Brush HeaderFGBrush { get; set; } = Brushes.Red;//test
public Brush HeaderBGBrush { get; set; } = Brushes.Red;//test
public string HeaderLine { get; set; } = "";
public string HeaderIcon { get; set; } = "";
public string Block { get; set; } = "";
public string Block_OneLine { get; set; } = "";
public int LineCount = 0;
public void Set_Header_Icon() => HeaderIcon = Header.Abbreviation + HeaderLine.Replace(Header.Title, "").Trim();
}
}
Here is the code for header:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace PHOWjection_Software_3._0.ItemType
{
public partial class Header : DataBaseItem
{
public Header Identity { get { return this; } }
private string _Title = ""; public string Title { get => _Title; set { _Title = value; PropertyChanged(DBProperties.Title); } }
private string _Abbreviation = ""; public string Abbreviation { get => _Abbreviation; set { _Abbreviation = value; PropertyChanged(DBProperties.Abbreviation); } }
public Brush BackGround_Brush { get; private set; }
private string _BackGround = AppColors.TransparentHex; public string BackGround
{
get => _BackGround; set
{
_BackGround = value;
BackGround_Brush = BackGround.ToBrush();
PropertyChanged(DBProperties.BackGround);
}
}
public Brush ForeGround_Brush { get; private set; }
private string _ForeGround = AppColors.TransparentHex; public string ForeGround
{
get => _ForeGround; set
{
_ForeGround = value;
ForeGround_Brush = ForeGround.ToBrush();
PropertyChanged(DBProperties.ForeGround);
}
}
}
}
The error that keeps appearing is
System.Windows.Markup.XamlParseException: 'Must create DependencySource on same Thread as the DependencyObject.'
Inner Exception
ArgumentException: Must create DependencySource on same Thread as the DependencyObject.
My code isn't muti-threading yet, Why does this error appear?