Back to Devexpress

ButtonEdit.ButtonTemplate Property

wpf-devexpress-dot-xpf-dot-editors-dot-buttonedit-ab76e9d9.md

latest8.7 KB
Original Source

ButtonEdit.ButtonTemplate Property

Gets or sets the data template used to render buttons of the current ButtonEdit.

Namespace : DevExpress.Xpf.Editors

Assembly : DevExpress.Xpf.Core.v25.2.dll

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
public DataTemplate ButtonTemplate { get; set; }
vb
Public Property ButtonTemplate As DataTemplate

Property Value

TypeDescription
DataTemplate

A DataTemplate object that is used to render buttons of the current ButtonEdit.

|

Remarks

The ButtonTemplate property specifies the data template used to render buttons from the ButtonEdit.ButtonsSource collection.

If you want to declare several data templates and apply one of them according to your own logic, use the ButtonEdit.ButtonTemplateSelector property.

Example

xaml
<dxe:ButtonEdit ButtonsSource="{Binding Items}">
    <dxe:ButtonEdit.ButtonTemplate>
        <DataTemplate>
            <ContentControl>
                <dxe:ButtonInfo Command="{Binding Command}"
                                Content="{Binding Content}"
                                IsLeft="{Binding IsLeft}"/>
            </ContentControl>
        </DataTemplate>
    </dxe:ButtonEdit.ButtonTemplate>
</dxe:ButtonEdit>
csharp
public class MainViewModel : ViewModelBase {

    private IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } }

    public ObservableCollection<ButtonViewModel> Items {
        get { return GetProperty(() => Items); }
        set { SetProperty(() => Items, value); }
    }
    public MainViewModel() {
        Items = new ObservableCollection<ButtonViewModel>() {
            new ButtonViewModel() {
                Content = "A",
                Command = new DelegateCommand(()=> {
                    MessageBoxService.ShowMessage("A");
                }),
                IsLeft = true,
            },
            new ButtonViewModel() {
                Content = "B",
                Command = new DelegateCommand(()=> {
                    MessageBoxService.ShowMessage("B");
                }),
                IsLeft = true,
            },
            new ButtonViewModel() {
                Content = "C",
                Command = new DelegateCommand(()=> {
                    MessageBoxService.ShowMessage("C");
                }),
                IsLeft = true,
            },
            new ButtonViewModel() {
                Content = "X",
                Command = new DelegateCommand(()=> {
                    MessageBoxService.ShowMessage("X");
                }),
            },
            new ButtonViewModel() {
                Content = "Y",
                Command = new DelegateCommand(()=> {
                    MessageBoxService.ShowMessage("Y");
                }),
            },
            new ButtonViewModel() {
                Content = "Z",
                Command = new DelegateCommand(()=> {
                    MessageBoxService.ShowMessage("Z");
                }),
            },
            new ButtonViewModel() {
                Content = "Clear",
                Command = new DelegateCommand(()=> {
                    this.Items.Clear();
                    Items = new ObservableCollection<ButtonViewModel>(Items);
                }),
            },
            new ButtonViewModel() {
                Content = "Add",
                Command = new DelegateCommand(()=> {
                    Items.Add(new ButtonViewModel() {
                        Content = "New",
                        IsLeft = this.Items.Count % 2 == 0
                    });
                    Items = new ObservableCollection<ButtonViewModel>(Items);
                }),
            },
        };
    }
}
vb
Public Class MainViewModel
    Inherits ViewModelBase

    Private ReadOnly Property MessageBoxService() As IMessageBoxService
        Get
            Return GetService(Of IMessageBoxService)()
        End Get
    End Property

    Public Property Items() As ObservableCollection(Of ButtonViewModel)
        Get
            Return GetProperty(Function() Items)
        End Get
        Set(ByVal value As ObservableCollection(Of ButtonViewModel))
            SetProperty(Function() Items, value)
        End Set
    End Property
    Public Sub New()
        Items = New ObservableCollection(Of ButtonViewModel)() From {
            New ButtonViewModel() With {
                .Content = "A",
                .Command = New DelegateCommand(Function() MessageBoxService.ShowMessage("A")),
                .IsLeft = True
            },
            New ButtonViewModel() With {
                .Content = "B",
                .Command = New DelegateCommand(Function() MessageBoxService.ShowMessage("B")),
                .IsLeft = True},
            New ButtonViewModel() With {
                .Content = "C",
                .Command = New DelegateCommand(Function() MessageBoxService.ShowMessage("C")),
                .IsLeft = True},
            New ButtonViewModel() With {
                .Content = "X",
                .Command = New DelegateCommand(Function() MessageBoxService.ShowMessage("X"))
            },
            New ButtonViewModel() With {
                .Content = "Y",
                .Command = New DelegateCommand(Function() MessageBoxService.ShowMessage("Y"))},
            New ButtonViewModel() With {
                .Content = "Z",
                .Command = New DelegateCommand(Function() MessageBoxService.ShowMessage("Z"))},
            New ButtonViewModel() With {
                .Content = "Clear",
                .Command = New DelegateCommand(
                    Sub()
                        Items.Clear()
                        Items = New ObservableCollection(Of ButtonViewModel)(Items)
                    End Sub)
            },
            New ButtonViewModel() With {
                .Content = "Add",
                .Command = New DelegateCommand(
                    Sub()
                        Items.Add(New ButtonViewModel() With {
                            .Content = "New",
                            .IsLeft = Items.Count Mod 2 = 0
                        })
                        Items = New ObservableCollection(Of ButtonViewModel)(Items)
                    End Sub)
            }
        }
    End Sub
End Class
csharp
public class ButtonViewModel : ViewModelBase {
    public string Content {
        get { return GetProperty(() => Content); }
        set { SetProperty(() => Content, value); }
    }
    public ICommand Command {
        get { return GetProperty(() => Command); }
        set { SetProperty(() => Command, value); }
    }
    public bool IsLeft {
        get { return GetProperty(() => IsLeft); }
        set { SetProperty(() => IsLeft, value); }
    }
}
vb
Public Class ButtonViewModel
    Inherits ViewModelBase

    Public Property Content() As String
        Get
            Return GetProperty(Function() Content)
        End Get
        Set(ByVal value As String)
            SetProperty(Function() Content, value)
        End Set
    End Property
    Public Property Command() As ICommand
        Get
            Return GetProperty(Function() Command)
        End Get
        Set(ByVal value As ICommand)
            SetProperty(Function() Command, value)
        End Set
    End Property
    Public Property IsLeft() As Boolean
        Get
            Return GetProperty(Function() IsLeft)
        End Get
        Set(ByVal value As Boolean)
            SetProperty(Function() IsLeft, value)
        End Set
    End Property
End Class

See Also

ButtonsSource

ButtonTemplateSelector

MVVM Framework

ButtonEdit Class

ButtonEdit Members

DevExpress.Xpf.Editors Namespace