Skip to content

Commit 01986a9

Browse files
Merge pull request #5600 from MicrosoftDocs/copilot/fix-5037
Add missing BindableAttribute information for C++/WinRT data binding in Windows App SDK documentation
2 parents 691d5d4 + 9e28f05 commit 01986a9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

hub/apps/develop/data-binding/data-binding-in-depth.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ In the two examples below, the `Button.Content` property is the binding target,
162162
<Button Content="{Binding ...}" ... />
163163
```
164164

165+
If you're using C++/WinRT, then you'll need to add the [**BindableAttribute**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.bindableattribute) attribute to any runtime class that you want to use the [{Binding}](/windows/uwp/xaml-platform/binding-markup-extension) markup extension with.
166+
167+
> [!IMPORTANT]
168+
> If you're using C++/WinRT, then the [**BindableAttribute**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.bindableattribute) attribute is available with Windows App SDK. Without that attribute, you'll need to implement the [ICustomPropertyProvider](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.icustompropertyprovider) and [ICustomProperty](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.icustomproperty) interfaces in order to be able to use the [{Binding}](/windows/uwp/xaml-platform/binding-markup-extension) markup extension.
169+
165170
### Binding object declared using {x:Bind}
166171

167172
There's one step we need to do before we author our [{x:Bind}](/windows/uwp/xaml-platform/x-bind-markup-extension) markup. We need to expose our binding source class from the class that represents our page of markup. We do that by adding a property (of type `HostViewModel` in this case) to our `MainWindow` window class.
@@ -234,6 +239,22 @@ Code to support `{x:Bind}` is generated at compile-time in the partial classes f
234239
235240
### Binding object declared using {Binding}
236241

242+
If you're using C++/WinRT then, to use the [{Binding}](/windows/uwp/xaml-platform/binding-markup-extension) markup extension, you'll need to add the [**BindableAttribute**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.bindableattribute) attribute to any runtime class that you want to bind to. To use [{x:Bind}](/windows/uwp/xaml-platform/x-bind-markup-extension), you don't need that attribute.
243+
244+
```cppwinrt
245+
// HostViewModel.idl
246+
// Add this attribute:
247+
[Microsoft.UI.Xaml.Data.Bindable]
248+
runtimeclass HostViewModel : Microsoft.UI.Xaml.Data.INotifyPropertyChanged
249+
{
250+
HostViewModel();
251+
String NextButtonText;
252+
}
253+
```
254+
255+
> [!IMPORTANT]
256+
> If you're using C++/WinRT, then the [**BindableAttribute**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.bindableattribute) attribute is available with Windows App SDK. Without that attribute, you'll need to implement the [ICustomPropertyProvider](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.icustompropertyprovider) and [ICustomProperty](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.icustomproperty) interfaces in order to be able to use the [{Binding}](/windows/uwp/xaml-platform/binding-markup-extension) markup extension.
257+
237258
[{Binding}](/windows/uwp/xaml-platform/binding-markup-extension) assumes, by default, that you're binding to the [**DataContext**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.frameworkelement.datacontext) of your markup window. So we'll set the `DataContext` of our window to be an instance of our binding source class (of type `HostViewModel` in this case). The example below shows the markup that declares the binding object. We use the same `Button.Content` binding target we used in the "Binding target" section earlier, and we bind to the `HostViewModel.NextButtonText` property.
238259

239260
``` xaml

0 commit comments

Comments
 (0)