Skip to content

Commit 2961685

Browse files
Merge pull request #1214 from telerik/new-kb-accessing-controls-inside-dynamic-radpopup-data-template-a4532a1e9638408fad3f7857263d1f81
Added new kb article accessing-controls-inside-dynamic-radpopup-data-template
2 parents b53cd00 + d0f5452 commit 2961685

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: Accessing Controls Inside Dynamic Popup Loaded from DataTemplate in ResourceDictionary
3+
description: Learn how to dynamically load Popup content from a DataTemplate in a ResourceDictionary in UI for .NET MAUI and update the controls inside.
4+
type: how-to
5+
page_title: Updating Controls Inside Popup Loaded Dynamically from DataTemplate in ResourceDictionary
6+
meta_title: Updating Controls Inside Popup Loaded Dynamically from DataTemplate in ResourceDictionary
7+
slug: accessing-controls-inside-dynamic-radpopup-data-template
8+
tags: popup, datatemplate, radpopup, maui, binding
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 11.0.1 | Popup for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |
17+
18+
## Description
19+
20+
I want to load a `RadPopup` dynamically using a `DataTemplate` defined in a `ResourceDictionary` and access or update the controls (e.g., `Label`) inside the template. However, accessing controls using `x:Name` is not working. I need a solution to update the control's properties, such as the `Text` property of a `Label`, when the popup opens.
21+
22+
This knowledge base article also answers the following questions:
23+
- How to bind and update controls in a `RadPopup` `DataTemplate` in .NET MAUI?
24+
- How to dynamically modify the content of `RadPopup` in .NET MAUI?
25+
- How to set control properties inside `RadPopup` using bindings or code?
26+
27+
## Solution
28+
29+
To achieve this, you can use one of two approaches:
30+
* [Binding the controls to a `ViewModel`](#option-1-using-binding-with-viewmodel)
31+
* [Programmatically accessing the controls inside the `DataTemplate`.](#option-2-accessing-controls-programmatically)
32+
33+
### Option 1: Using Binding with ViewModel
34+
35+
Use bindings to update the control properties inside the `DataTemplate`. Bind the `Label`'s `Text` property to a `ViewModel` and ensure the binding mode is set to `TwoWay`.
36+
37+
**1.** Define a `ViewModel` with a property for binding:
38+
39+
```csharp
40+
public class ViewModel : NotifyPropertyChangedBase
41+
{
42+
private string myValue;
43+
44+
public ViewModel()
45+
{
46+
this.myValue = "hello";
47+
}
48+
49+
public string MyValue
50+
{
51+
get => this.myValue;
52+
set => this.UpdateValue(ref this.myValue, value);
53+
}
54+
}
55+
```
56+
57+
**2.** Set the `BindingContext` in your code-behind:
58+
59+
```csharp
60+
public partial class MainPage : ContentPage
61+
{
62+
ViewModel vm;
63+
64+
public MainPage()
65+
{
66+
InitializeComponent();
67+
this.vm = new ViewModel();
68+
this.BindingContext = this.vm;
69+
}
70+
71+
private void CheckBoxIsCheckedChanged(object sender, IsCheckedChangedEventArgs e)
72+
{
73+
if (e.NewValue == true)
74+
{
75+
this.popup.IsOpen = true;
76+
this.vm.MyValue = "value changed";
77+
}
78+
}
79+
}
80+
```
81+
82+
**3.** Define the `DataTemplate` in XAML and bind the `Label`'s `Text` property:
83+
84+
```xml
85+
<DataTemplate x:Key="PopupTemplate">
86+
<telerik:RadBorder CornerRadius="8"
87+
BackgroundColor="#F9F9F9"
88+
BorderColor="Red"
89+
BorderThickness="1"
90+
WidthRequest="300"
91+
Padding="12">
92+
<VerticalStackLayout Spacing="8">
93+
<Label Text="{Binding MyValue, Mode=TwoWay}" />
94+
</VerticalStackLayout>
95+
</telerik:RadBorder>
96+
</DataTemplate>
97+
```
98+
99+
### Option 2: Accessing Controls Programmatically
100+
101+
Directly access the controls inside the `DataTemplate` programmatically and update their properties.
102+
103+
**1.** Define the `DataTemplate` in XAML:
104+
105+
```xml
106+
<DataTemplate x:Key="PopupTemplate">
107+
<telerik:RadBorder CornerRadius="8"
108+
BackgroundColor="#F9F9F9"
109+
BorderColor="Red"
110+
BorderThickness="1"
111+
WidthRequest="300"
112+
Padding="12">
113+
<VerticalStackLayout Spacing="8">
114+
<Label />
115+
</VerticalStackLayout>
116+
</telerik:RadBorder>
117+
</DataTemplate>
118+
```
119+
120+
**2.** In the code-behind, dynamically load the content and update the control properties:
121+
122+
```csharp
123+
private void CheckBoxIsCheckedChanged(object sender, IsCheckedChangedEventArgs e)
124+
{
125+
if (e.NewValue == true)
126+
{
127+
this.popup.IsOpen = true;
128+
var dataTemplate = this.myPage.Resources["PopupTemplate"] as DataTemplate;
129+
130+
var popupcontent = dataTemplate.CreateContent() as View;
131+
this.popup.Content = popupcontent;
132+
133+
List<IVisualTreeElement> items = (List<IVisualTreeElement>)this.popup.Content.GetVisualTreeDescendants();
134+
foreach (IVisualTreeElement myControl in items)
135+
{
136+
if (myControl is Label)
137+
{
138+
Label control = (Label)myControl;
139+
control.Text = "popup content has been changed when opened";
140+
return;
141+
}
142+
}
143+
}
144+
}
145+
```
146+
147+
### Key Considerations:
148+
149+
- Accessing controls inside a `DataTemplate` using `x:Name` is not possible due to the general behavior of the MAUI framework.
150+
- Use bindings if you want to follow MVVM principles.
151+
- For direct control access, ensure the `DataTemplate` content is created and assigned to the popup before attempting to modify controls.
152+
153+
## See Also
154+
155+
- [.NET MAUI Popup Documentation](https://docs.telerik.com/devtools/maui/controls/popup/overview)

0 commit comments

Comments
 (0)