user1219310
user1219310

Reputation: 732

Button command not working in DataTemplate

I have created a simple test solution to test the button in data template and set it to ContentControl. However not working and response in current implementation.

MainWindow.xam

<Window x:Class="ButtonCommandWithDataTemplate.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ux="clr-namespace:ButtonCommandWithDataTemplate"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="SimpleView" DataType="{x:Type ux:InnerViewModel}">
            <Button Command="{Binding ClickCommand}" Content="Click" />
        </DataTemplate>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ContentControl >
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Setter Property="ContentTemplate" Value="{StaticResource SimpleView}" />
            </Style>
        </ContentControl.Style>
    </ContentControl>
</Grid>

MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ButtonCommandWithDataTemplate
{
    public class MainWindowViewModel
    {
        private InnerViewModel innerVm;

        public InnerViewModel InnerVm
        {
            get { return innerVm; }
            set { innerVm = value; }
        }

        public MainWindowViewModel()
        {
            this.innerVm = new InnerViewModel();
        }
    }
}

InnerViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ButtonCommandWithDataTemplate
{
    public class InnerViewModel
    {
        private ICommand clickCommand;

        public ICommand ClickCommand
        {
            get
            {
                if (this.clickCommand == null)
                {
                    this.clickCommand = new RelayCommand(param =>
                    {
                        try
                        {
                            MessageBox.Show("W0w");
                        }
                        catch (Exception exception)
                        {
                            throw exception;
                        }
                    });
                }

                return this.clickCommand;
            }
        }
    }
}

Download project

Upvotes: 1

Views: 1279

Answers (1)

jsw
jsw

Reputation: 175

In your project that I downloaded you had this

<DataTemplate x:Key="SimpleView">
            <Button Command="{Binding Path=DataContext.ClickCommand, 
            RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
            Content="Click" />
</DataTemplate>

I changed it to this and it works for me

<DataTemplate x:Key="SimpleView">
            <Button Command="{Binding Path=DataContext.InnerVm.ClickCommand, 
            RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
            Content="Click" />
</DataTemplate>

The DataContext is MainViewModel which didn't have a ClickCommand, it is on the InnerViewModel.

Upvotes: 2

Related Questions