Binding a button in a DataTemplate to a Viewmodel Relay command

Marc Jeeves 386 Reputation points
2021-06-19T21:03:49.85+00:00

I cant get the buttons to bind to the Btn_AddNewDataModel_Click relay command can anybody help and also i need to send the CommandParameter model back any suggestions

Thanks

Madaxe

    <Grid>
        <Expander Header="Software" IsExpanded="True">
            <Grid Margin="40,10,0,0">
                <ItemsControl ItemsSource="{Binding Path=Media_Model.Vendors}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Expander Header="{Binding Name}" Margin="0,0,0,20">
                                <Grid Margin="20,10,0,0">
                                    <ItemsControl ItemsSource="{Binding Software}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Button Content="{Binding Name}" 
                                                        Width="100" 
                                                        HorizontalAlignment="Left" 
                                                        Margin="5,5,5,5"
                                                        Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=Btn_AddNewDataModel_Click}"
                                                        CommandParameter="{Binding}"
                                                        />
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </Grid>
                            </Expander>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>
        </Expander>
    </Grid>


public class SoftwareInstallViewModel
    {
        private string _ConfigurationXMLPath = @"M:\Nikola\SoftwareList.json";
        public Media_Model Media_Model { get; set; } = null;
        public RelayCommand Btn_AddNewDataModel_Click { get; private set; }


        public SoftwareInstallViewModel()
        {
            LoadConfigurationXML();

            Btn_AddNewDataModel_Click = new RelayCommand(AddNewDataModel, CanAddNewDataModel);
        }

        public void AddNewDataModel(object message) 
        { 

        }
        public bool CanAddNewDataModel(object message) 
        {
            return true;
        }


        private void LoadConfigurationXML()
        {
            using (StreamReader streamReader = new StreamReader(_ConfigurationXMLPath))
            {
                string json = streamReader.ReadToEnd();
                Media_Model = JsonConvert.DeserializeObject<Media_Model>(json);
            }
        }
    }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
{count} votes

1 additional answer

Sort by: Most helpful
  1. Marc Jeeves 386 Reputation points
    2021-06-19T22:05:44.883+00:00

    I was able to resolve the binding issue by navigating the ancestor to the window, since the datacontext is at the window level this works, thanks for the hint.

    I now want to bind the button tag the the Software model any ideas?

    Thansk

    Madaxe

    <Window x:Class="Nikola_Software_Installation_App.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Nikola_Software_Installation_App"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Expander Header="Software" IsExpanded="True">
                <Grid Margin="40,10,0,0">
                    <ItemsControl ItemsSource="{Binding Path=Media_Model.Vendors}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Expander Header="{Binding Name}" Margin="0,0,0,20">
                                    <Grid Margin="20,10,0,0">
                                        <ItemsControl ItemsSource="{Binding Software}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <Button Content="{Binding Name}" 
                                                            Width="100" 
                                                            HorizontalAlignment="Left" 
                                                            Margin="5,5,5,5"
                                                            Tag="{Binding Software}"
                                                            Command="{Binding DataContext.Btn_AddNewDataModel_Click, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
                                                            CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
                                                            />
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </Grid>
                                </Expander>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </Expander>
        </Grid>
    </Window>