-1

I have a WPF app with a Treeview and a detail page. The detail page is done via several dictionaries to handle the different layouts. Now I would like to add to the detail a edit and save button. I can bind it to the model as the data but then I have a problem how to communicate saves from the model to the viewmodel.

But if I put in the dictionary directly the name of a method that is in the code behind of the view I get an error at runtime.

This is the TreeView in the MasterDetailPage.xaml:

        <TreeView
        x:Name="TreeView"
        Grid.Row="1"
        ItemTemplate="{StaticResource PAZHTemplate}"
        ItemsSource="{Binding Patients}"
        SelectedItemChanged="select_SelectedItemChanged"
        TreeViewItem.Expanded="ExpandItem" />

This is the details element:

    <ContentControl
    Grid.Column="2"
    Margin="{StaticResource XSmallLeftTopMargin}"
    Content="{Binding Selected}"
    ContentTemplateSelector="{StaticResource ContentTemplateSelector}" />

Here we go with the Selector:

   <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="TitoDoc\TPazDictionary.xaml" />
    <ResourceDictionary Source="TitoDoc\TAppDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>

<templateSelectors:TreeViewDataContentTemplateSelector
    x:Key="ContentTemplateSelector"
    APPTemplate="{StaticResource APPContentTemplate}"
    APRTemplate="{StaticResource APRContentTemplate}"/>

And here the TPazDictionary.xaml

            <Button
                x:Name="Save"
                Grid.Column="1"
                Content="&#xE74E;"
                Command="{Binding CmdSaveSwitch, Mode=OneWay}"
                ContentTemplate="{StaticResource IconFilterButton}"
                DockPanel.Dock="Right"
                ToolTipService.ToolTip="Save"
                Visibility="{Binding IsVisible}" />
            <Button
                x:Name="LockUnlock"
                Grid.Column="2"
                Command="{Binding CmdEditSwitch, Mode=OneWay}"
                Content="{Binding Icon}"
                ContentTemplate="{StaticResource IconFilterButton}"
                DockPanel.Dock="Right"
                ToolTipService.ToolTip="Edit" />

        </Grid>
        <Grid Margin="0,6,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" MinWidth="40" />
                <ColumnDefinition Width="*" MinWidth="40" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" MinHeight="12" />
                <RowDefinition Height="Auto" MinHeight="12" />
            </Grid.RowDefinitions>
            <TextBlock
                Grid.Row="0"
                Grid.Column="0"
                Padding="2"
                Style="{DynamicResource ContentLabel}"
                Text="Cognome" />
            <TextBox
                Grid.Row="1"
                Grid.Column="0"
                IsReadOnly="{Binding IsReadOnly}"
                Text="{Binding Paz.Cognome}" />
            <TextBlock
                Grid.Row="0"
                Grid.Column="1"
                Style="{DynamicResource ContentLabel}"
                Text="Nome" />
            <TextBlock
                Grid.Row="1"
                Grid.Column="1"
                Style="{DynamicResource ContentData}"
                Text="{Binding Paz.Nome}" />
        </Grid>
    </StackPanel>
</ScrollViewer>

This is the binding with the model:

                Command="{Binding CmdSaveSwitch, Mode=OneWay}"

but if I put

                Command="Save_Click"

I get the error even if in MasterDetailPage.xaml.cs the method is defined:

    private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
Clemens
  • 123,504
  • 12
  • 155
  • 268

2 Answers2

0

Only properties of types that implement the ICommand interface can be used to bind commands, not methods.

AdiletB
  • 77
  • 3
  • Sorry so I'm doing something wrong in the declaration of Save_Click? The binding works fine but the method is in the Model so its not where I want it – Massimo Savazzi Oct 26 '20 at 17:31
0

Event handlers must be defined in the same class as the XAML markup, i.e. you cannot define the event handler Save_Click in MasterDetailPage.xaml.cs if you add Command="Save_Click" to TPazDictionary.xaml or any other file than MasterDetailPage.xaml.

You may add a code-behind file to the TPazDictionary.xaml resource dictionary and define the event handler in there though.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • ouch! but the code behind the TPazDictionary.xaml will not have complete visibility of properties and methods in the MasterDetailPage.xaml.cs ... as the Dictionary was a trick to split big XAML files I was hoping that the code behind the parent page was able to connect to the correct properties – Massimo Savazzi Oct 26 '20 at 17:29
  • @MassimoSavazzi: Well, now you know that's not the case. – mm8 Oct 26 '20 at 20:46