ReadOnlyDependencyPropertyBindingBehavior Class
Allows you to bind read-only dependency and attached properties to a ViewModel’s properties.
Namespace: DevExpress.Mvvm.UI
Assembly: DevExpress.Xpf.Core.v21.1.dll
NuGet Package: DevExpress.Wpf.Core
Declaration
Remarks
The following properties are required to use ReadOnlyDependencyPropertyBindingBehavior:
Property | Description |
---|---|
Binding | Gets or sets a binding that should be applied to the specified property. This is a dependency property. |
Property or DependencyProperty | Gets or sets the bound property. This is a dependency property. |
The following table lists additional customization properties:
Property | Description |
---|---|
Command | Gets or sets the command that the ReadOnlyDependencyPropertyBindingBehavior should execute when the bound property’s value is changed (specified in the Property or DependencyProperty). This is a dependency property. |
IsEnabled | Gets or sets whether a bound ViewModel’s property should be updated. This is a dependency property. |
Follow the steps below to bind a read-only dependency property to a ViewModel’s property:
Attach the behavior to a target control and choose one of the following options:
Set the Property to a target property’s name:
<TreeView> <dxmvvm:Interaction.Behaviors> <dxmvvm:ReadOnlyDependencyPropertyBindingBehavior Property="SelectedItem" /> </dxmvvm:Interaction.Behaviors> </TreeView>
Use the DependencyProperty to specify a target dependency/attached property:
<TreeView> <dxmvvm:Interaction.Behaviors> <dxmvvm:ReadOnlyDependencyPropertyBindingBehavior DependencyProperty="{x:Static TreeView.SelectedItemProperty}" /> </dxmvvm:Interaction.Behaviors> </TreeView>
Use the behavior’s Binding property to specify a binding to the target ViewModel’s property.
<TreeView> <dxmvvm:Interaction.Behaviors> <dxmvvm:ReadOnlyDependencyPropertyBindingBehavior Binding="{Binding SelectedMenuItem, Mode=OneWayToSource}" DependencyProperty="{x:Static TreeView.SelectedItemProperty}" /> </dxmvvm:Interaction.Behaviors> </TreeView>
The following code sample binds the TreeView.SelectedItem read-only property to the ViewModel’s SelectedMenuItem property:
<UserControl x:Class="DXSample.Views.MainView"
<!-- ... -->
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
<!-- ... -->
xmlns:ViewModels="clr-namespace:DXSample.ViewModels"
<!-- ... -->
<UserControl.DataContext>
<ViewModels:MainViewModel />
</UserControl.DataContext>
<DockPanel>
<TreeView Name="view" ItemsSource="{Binding Menu}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type ViewModels:MenuItemViewModel}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Title}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
<dxmvvm:Interaction.Behaviors>
<dxmvvm:ReadOnlyDependencyPropertyBindingBehavior Binding="{Binding SelectedMenuItem, Mode=OneWayToSource}"
DependencyProperty="{x:Static TreeView.SelectedItemProperty}" />
</dxmvvm:Interaction.Behaviors>
</TreeView>
</DockPanel>
</UserControl>
using System.Collections.ObjectModel;
using System.Windows;
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
// ...
public class MainViewModel : ViewModelBase {
public MenuItemViewModel SelectedMenuItem {
get { return GetProperty(() => SelectedMenuItem); }
set { SetProperty(() => SelectedMenuItem, value); }
}
[Command]
public void Open() {
// ...
}
// ...
}