I want to get and set the AppendChild value that I added to DataTemplate

Asked 2 years ago, Updated 2 years ago, 122 views

C#WPF Code Template
You have created and displayed a DataTemplate using the above page as a reference.

FrameworkElementFactory textBlock = newFrameworkElementFactory(typeof(TextBlock), "TextBlock");
textBlock.SetValue (TextBlock.MarginProperty, new Thickness(5,5,5,5));
textBlock.SetValue (TextBlock.TextProperty, "text body");

FrameworkElementFactory image=new FrameworkElementFactory(typeof(Image), "Image");
BitmapImage bitmapImage = new BitmapImage (new Uri);
imageElement.SetValue(Image.SourceProperty, bitmapImage);

FrameworkElementFactory stackPanel=new FrameworkElementFactory(typeof(StackPanel), "StackPanel");
stackPanel.AppendChild(textBlock);
stackPanel.AppendChild(image);

DataTemplate template = new DataTemplate();
template.VisualTree=stackPanel;

What should I do if I want to change the text body or bitmapImage?
Also, is it possible to retrieve and configure AppendChild elements from DataTemplate?

c# wpf

2022-09-30 21:30

1 Answers

Since DataTemplate, I think you should first use DataContext to manipulate properties using BindingBase.

textBlock.SetBinding(TextBlock.TextProperty, new Binding("Properties to Bind"));

You can also use the event handler

 imageElement.AddHandler (FrameworkElement.LoadedEvent, (RoutedEventHandler) imageElement_Loaded);

static void imageElement_Loaded (object sender, RoutedEventArgse)
{
    var imageElement=(Image) sender;
    // some sort of treatment
}


2022-09-30 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.