C
C#15mo ago
Alix

❔ update observeral collection into ui

i got a question since i got this code
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; j < currentBlockCount - i; j++)
{
var blockinner = blocksCollection[j];

if (((SolidColorBrush)blockinner.Background).Color == ((SolidColorBrush)block.Background).Color)
{
for (int k = i + 2; k < j; k++)
{
var NextBlock = blocksCollection[k + 1];
var CurrentBlock = blocksCollection[k];
if (((SolidColorBrush)CurrentBlock.Background).Color != ((SolidColorBrush)NextBlock.Background).Color)
{
blocksCollection.RemoveAt(k);
blocksCollection.Insert(k - 1, CurrentBlock);
Console.WriteLine($"Swapped {k} with {k - 1} ");
}
}
}
}
}
}
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; j < currentBlockCount - i; j++)
{
var blockinner = blocksCollection[j];

if (((SolidColorBrush)blockinner.Background).Color == ((SolidColorBrush)block.Background).Color)
{
for (int k = i + 2; k < j; k++)
{
var NextBlock = blocksCollection[k + 1];
var CurrentBlock = blocksCollection[k];
if (((SolidColorBrush)CurrentBlock.Background).Color != ((SolidColorBrush)NextBlock.Background).Color)
{
blocksCollection.RemoveAt(k);
blocksCollection.Insert(k - 1, CurrentBlock);
Console.WriteLine($"Swapped {k} with {k - 1} ");
}
}
}
}
}
}
else
{
MessageBox.Show("No blocks to sort.");
}
}
i want that it sort the blocks into my ui but the blocks are not updating in my ui but it should update right? because i replace it with other block? so my question is how could i update a observeral collection list in my ui
No description
13 Replies
Alix
AlixOP15mo ago
could someone help me with this
Kouhai
Kouhai15mo ago
Can you show your bindings with the ObservableCollection?
Alix
AlixOP15mo ago
atm i'm not sure how to work with binding or do you mean like Get; set;
Alix
AlixOP15mo ago
but i have it now like this
No description
Alix
AlixOP15mo ago
and this is my xaml
No description
Kouhai
Kouhai15mo ago
@alixd91 So you're not binding to the collection? Yeah I don't see any binding in the XAML
Alix
AlixOP15mo ago
should i be something like this then?]
No description
No description
Alix
AlixOP15mo ago
because i need to add the children.add
private void GenerateBlocks(object sender, RoutedEventArgs e)
{
var input = NumberBox.Text;
if (int.TryParse(input, out int numberOfBlocks))
{

for (int i = 0; i < numberOfBlocks; i++)
{
TextBlock txblock = new TextBlock();
txblock.Background = items[random.Next(items.Count)];
txblock.Height = 40;
txblock.Width = 40;
txblock.Margin = new Thickness(top, left, right, bottom);
blocksCollection.Add(txblock);
Blockstime.Children.Add(txblock);
}
}
else
{
MessageBox.Show("Please enter a valid number.");
}
}
private void GenerateBlocks(object sender, RoutedEventArgs e)
{
var input = NumberBox.Text;
if (int.TryParse(input, out int numberOfBlocks))
{

for (int i = 0; i < numberOfBlocks; i++)
{
TextBlock txblock = new TextBlock();
txblock.Background = items[random.Next(items.Count)];
txblock.Height = 40;
txblock.Width = 40;
txblock.Margin = new Thickness(top, left, right, bottom);
blocksCollection.Add(txblock);
Blockstime.Children.Add(txblock);
}
}
else
{
MessageBox.Show("Please enter a valid number.");
}
}
like this but when i do it like this it doesn't work
<ItemsControl Grid.Row="1" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="Blockstime" VerticalAlignment="Top" MinHeight="250" Width="Auto" Orientation="Horizontal" IsItemsHost="True">

</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<ItemsControl Grid.Row="1" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="Blockstime" VerticalAlignment="Top" MinHeight="250" Width="Auto" Orientation="Horizontal" IsItemsHost="True">

</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Kouhai
Kouhai15mo ago
So blocksCollection is a collection of TextBlock? I think you're misunderstanding how ObservableCollection works, observable collection only works when a control is bound to it
Alix
AlixOP15mo ago
so iwhat i just want is that the blocks that are in my observeral collection i want to update in my gui since i update it in my collection but not in my gui
Kouhai
Kouhai15mo ago
You can bind ItemsControl to the collection and in the template generate the blocks The collection would be of brushes
Alix
AlixOP15mo ago
there is also a way to send the updates of the observeral collection to the ui right without using binding? to just push the new updates to it
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