C
C#9mo ago
Alix

❔ replace oldblockscollection with new blockscollection

hey since my sorting is finished i want to make it work in my ui
private void SortBlocks(object sender, RoutedEventArgs e)
{
int currentBlockCount = blocksCollection.Count;

if (currentBlockCount > 0)
{
for (int i = 0; i < currentBlockCount; i++)
{
var block = blocksCollection[i];
Console.WriteLine($"i ={i}");

Console.WriteLine(((SolidColorBrush)block.Background).Color.ToString());
for (int j = i + 1; j < currentBlockCount; j++)
{
var blockinner = blocksCollection[j];
if (((SolidColorBrush)blockinner.Background).Color == ((SolidColorBrush)block.Background).Color)
{
if (blockinner == blocksCollection[i + 1])
{
break;
}
else
{
List<TextBlock> blocks = new List<TextBlock>();
for (int k = i + 1; k < j; k++)
{
blocks.Add(blocksCollection[k]);
}
blocksCollection[i + 1] = blocksCollection[i];
int m = 0;
for (int l = i + 2; l < j + 1; l++)
{
blocksCollection[l] = blocksCollection[m];
m++;
}
}
}
}
}
}
else
{
MessageBox.Show("No blocks to sort.");
}
}
private void SortBlocks(object sender, RoutedEventArgs e)
{
int currentBlockCount = blocksCollection.Count;

if (currentBlockCount > 0)
{
for (int i = 0; i < currentBlockCount; i++)
{
var block = blocksCollection[i];
Console.WriteLine($"i ={i}");

Console.WriteLine(((SolidColorBrush)block.Background).Color.ToString());
for (int j = i + 1; j < currentBlockCount; j++)
{
var blockinner = blocksCollection[j];
if (((SolidColorBrush)blockinner.Background).Color == ((SolidColorBrush)block.Background).Color)
{
if (blockinner == blocksCollection[i + 1])
{
break;
}
else
{
List<TextBlock> blocks = new List<TextBlock>();
for (int k = i + 1; k < j; k++)
{
blocks.Add(blocksCollection[k]);
}
blocksCollection[i + 1] = blocksCollection[i];
int m = 0;
for (int l = i + 2; l < j + 1; l++)
{
blocksCollection[l] = blocksCollection[m];
m++;
}
}
}
}
}
}
else
{
MessageBox.Show("No blocks to sort.");
}
}
12 Replies
Alix
Alix9mo ago
but how could i make that i could replace my old blocks with my new blockscollection without clearing my ui field with blockstime.children.x
JakenVeina
JakenVeina9mo ago
my ui field
define this
Alix
Alix9mo ago
i defined it
No description
JakenVeina
JakenVeina9mo ago
okay.... but you hadn't posted it here so, how does the above code translate to anything in that wrap panel?
Alix
Alix9mo ago
Nothing I firstly fixed that everything what sorted correctly in the observeral collection And after that i wanted to fix that it shows in ui but i’m not sure how to make that i replace the old observeral collection with the new one without clearing the wrappanel and just replacing it with correct positions
JakenVeina
JakenVeina9mo ago
what observable collection?
Alix
Alix9mo ago
private ObservableCollection<TextBlock> blocksCollection { get; set; } = new ObservableCollection<TextBlock>();
private ObservableCollection<TextBlock> blocksCollection { get; set; } = new ObservableCollection<TextBlock>();
i just need to know how to render the updated observable collection
JakenVeina
JakenVeina9mo ago
nooooooooot that way A)
public ObservableCollection<string> Blocks { get; }
public ObservableCollection<string> Blocks { get; }
B)
<ItemsControl ItemsSource="{Binding Blocks}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding Blocks}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
C) that's it the entire POINT of an ObservableCollection is that it's... observable you don't need to trigger render updates when you change it the collection does that itself and the UI layer listens to that that's what the Binding system accomplishes you add an item to the collection, it appears on the screen you remove an item from the collection, it vanishes from the screen you sort the collection, the UI updates to match
Alix
Alix9mo ago
is it a big problem if i make the string to a textblock since i need to use that to get background etc?
JakenVeina
JakenVeina9mo ago
philosophically, yes, it's a code smell that you're mixing rendering concerns with logical concerns mechanically, you're probably gonna have trouble actually getting those TextBlocks to DISPLAY, since the binding system isn't really built for that you're effectively trying to bypass the ItemTemplate step, and I don't know if there's a mechanism to do that leaving the ItemTemplate unspecified MIGHT work?
Alix
Alix9mo ago
alright it works Thank you yeah i see he is now complaining about this System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='TextBlock'
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.