© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
10 replies
Davaaron

❔ WPF - Use command of viewmodel instead of item on click

Hi,
I have a list of strings and display them as buttons. The list is held by a viewmodel and the viewmodel has a Command for the button click. Now, instead of creating an object for strings and add the Command to the items, I would like to have the Command at the viewmodel level.

  <Grid x:Name="LayoutRoot" Margin="5">
        <ItemsControl ItemsSource="{Binding Options}" Grid.Row="1">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Command="{Binding CloseDialogCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type vm:NotificationDialogViewModel}}}" CommandParameter="true" Content="{Binding }" MinWidth="75" Height="25" Margin="10,5,10,5" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
  <Grid x:Name="LayoutRoot" Margin="5">
        <ItemsControl ItemsSource="{Binding Options}" Grid.Row="1">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Command="{Binding CloseDialogCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type vm:NotificationDialogViewModel}}}" CommandParameter="true" Content="{Binding }" MinWidth="75" Height="25" Margin="10,5,10,5" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>



  public class NotificationDialogViewModel : BindableBase, IDialogAware
  {
        private DelegateCommand<string> _closeDialogCommand;
        public DelegateCommand<string> CloseDialogCommand =>
            _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog));

        protected virtual void CloseDialog(string parameter)
        {
            ButtonResult result = ButtonResult.None;

            if (parameter?.ToLower() == "true")
                result = ButtonResult.OK;
            else if (parameter?.ToLower() == "false")
                result = ButtonResult.Cancel;

            RaiseRequestClose(new DialogResult(result, new DialogParameters($"parameter={parameter}")));
        }

  }
  public class NotificationDialogViewModel : BindableBase, IDialogAware
  {
        private DelegateCommand<string> _closeDialogCommand;
        public DelegateCommand<string> CloseDialogCommand =>
            _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog));

        protected virtual void CloseDialog(string parameter)
        {
            ButtonResult result = ButtonResult.None;

            if (parameter?.ToLower() == "true")
                result = ButtonResult.OK;
            else if (parameter?.ToLower() == "false")
                result = ButtonResult.Cancel;

            RaiseRequestClose(new DialogResult(result, new DialogParameters($"parameter={parameter}")));
        }

  }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Command WPF with viewmodel
C#CC# / help
4y ago
❔ WPF Use of multiple item controls
C#CC# / help
4y ago
WPF running .ToString() of viewmodel instead of showing the coresponding view
C#CC# / help
16mo ago
❔ WPF binding expression of property from ItemsControl item with viewmodel property
C#CC# / help
4y ago