question

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 asked EmonHaque-1485 edited

Binding a button in a DataTemplate to a Viewmodel Relay command

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-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Don't forget DataContext. prefix in Path when you're in a DataTemplate.

0 Votes 0 ·
MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 answered

ok im all set this is the fix for the tag

Tag="{Binding BindsDirectlyToSource=True}"

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 answered EmonHaque-1485 edited

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>
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If the Name and Software properties are available in Media_Model.Vendors, it'll work.

1 Vote 1 ·