❔ WPF edit Label Content

Hey, I want to set the label on a random position. After this I want to edit the content of the label, buy how can I do that if my label are object or UIElement
foreach (var OR UIElement label in RememberScreen.Children)
if (typeof(Label).IsInstanceOfType(label))
{
while (true)
{
// increment i
i++;
// set position from left
posL ......
// set position from top
posT = .....
Debug.WriteLine(posL + " " + posT);

if (oldPosT.Contains(posT) != true)
{
label.Content = $"{i}"; // HERE IS THE ERROR
Canvas.SetLeft(labels[i], posL);
Canvas.SetTop(labels[i], posT);
label.Visibility = Visibility.Visible;
// add the position to the list
oldPosT.Add(posT);
Debug.WriteLine(oldPosT[i]);
break;
}
foreach (var OR UIElement label in RememberScreen.Children)
if (typeof(Label).IsInstanceOfType(label))
{
while (true)
{
// increment i
i++;
// set position from left
posL ......
// set position from top
posT = .....
Debug.WriteLine(posL + " " + posT);

if (oldPosT.Contains(posT) != true)
{
label.Content = $"{i}"; // HERE IS THE ERROR
Canvas.SetLeft(labels[i], posL);
Canvas.SetTop(labels[i], posT);
label.Visibility = Visibility.Visible;
// add the position to the list
oldPosT.Add(posT);
Debug.WriteLine(oldPosT[i]);
break;
}
Can I replace the label from my canvas with a new label than it's exactly the same with different content?
7 Replies
ero
ero2y ago
foreach (UIElement element in RememberScreen.Children)
{
if (element is not Label label)
continue;

// do things with `label`
}
foreach (UIElement element in RememberScreen.Children)
{
if (element is not Label label)
continue;

// do things with `label`
}
EinfachNurBaum
EinfachNurBaumOP2y ago
You get only the UIElement of the labels and UIElement has not Content. This is the shit
ero
ero2y ago
which is why you cast it to Label like i did in the code i sent
EinfachNurBaum
EinfachNurBaumOP2y ago
what? every element is a UIElement, can I just make (Label) like (int) or what do you mean?
ero
ero2y ago
you can just do what i posted
EinfachNurBaum
EinfachNurBaumOP2y ago
c# is strange but looks like it works but thank you
Accord
Accord2y 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