C
C#15mo 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
AlixOP15mo 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
JakenVeina15mo ago
my ui field
define this
Alix
AlixOP15mo ago
i defined it
No description
JakenVeina
JakenVeina15mo ago
okay.... but you hadn't posted it here so, how does the above code translate to anything in that wrap panel?
Alix
AlixOP15mo 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
JakenVeina15mo ago
what observable collection?
Alix
AlixOP15mo 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
JakenVeina15mo 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
AlixOP15mo 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
JakenVeina15mo 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
AlixOP15mo 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
Accord15mo 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.
Want results from more Discord servers?
Add your server