Click here to Skip to main content
15,942,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having problem triggering button which place inside a Datatemplate which is designed for render listboxitems.

listboxitem has a data source as shown
public class WordListItem : INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string info)
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(info));

        private string word;
        private int count;

        public string Word
            get { return word; }
                if(value != word)
                    word = value;

        public int Count
            get { return count; }
                if (value != count)
                    count = value;


and I have a datatemplate to render above data as listboxitem :
<ResourceDictionary xmlns=""

<DataTemplate x:Key="ListItemStyle" DataType="{x:Type M:WordListItem}" >
                <Border BorderThickness="1" BorderBrush="Gray" 
                     Padding="7" Name="border" Margin="3">
                <Grid >
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition Width="*"/>

                    <!--star mark-->
                    <Polygon Grid.Row="0" Grid.Column="0" Grid.RowSpan="2"
                             Fill="Yellow" Stroke="Black" StrokeThickness="1"
                             StrokeLineJoin="Round" Width="20" Height="20"
                             Points="9,2 11,7 17,7 12,10 14,15 9,12 4,15 6,10 1,7 7,7"
                             Visibility="Hidden" Name="star"/>
                    <!-- word-->
                    <TextBlock Grid.Row="0" Grid.Column="1" Margin="0,0,8,0"
                                 Text="{Binding Path=Word}"
                    <!--infor abour that word, display below word-->
                    <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
                        <TextBlock Text="Times" />
                        <TextBlock Text="{Binding Path=Count}" Name="TotalChecking"/>

                    <Button  Command="{Binding Path= ButtonClick}">

                    <CheckBox Content="TestCH" removed="AliceBlue"/>



as show in code, the datatemplate bound to attributes of WordListItem to render items in listbox. but I want to bind the command of Button ( which place in datatemplate ) to Viewmodel, its code is below:
public class WordListItemViewModel : ViewModelBase

        private ObservableCollection<WordListItem> _WordItems;

        public WordListItemViewModel()
            if (_WordItems == null)
                _WordItems = new ObservableCollection<WordListItem>();


        public WordListItemViewModel(ObservableCollection<WordListItem> _wordItem)
            if (_wordItem == null)
                throw new Exception("word item is Null");
            if (_WordItems == null)
                _WordItems = new ObservableCollection<WordListItem>();
            _WordItems = _wordItem;

           _ButtonClick = new RelayCommand(param =>this.BtClick());

        public ObservableCollection<WordListItem> WordItmes
            get { return _WordItems; }
                if (value != _WordItems)
                    _WordItems = value;

        private static RelayCommand _ButtonClick;
        public  ICommand ButtonClick
                if (_ButtonClick == null)
                    _ButtonClick = new RelayCommand(param => this.BtClick(),param=>this.CanClick);
                return _ButtonClick;

        private void BtClick()
            MessageBox.Show("button inside ListItem is clicked!");

        private bool CanClick
            get { return true; }

untill now DataTemplate render items well, but I am having problem to binding ButtonClick (which inside ViewModel) to button (which inside datatemplate).I think it is not good approach to binding button to command inside WordListItem, I should bind it to command inside ViewModel.

is that so difficult?

could anyone help me with this? I am a beginner in VMMV, so please help me with detailed answer or articles.

thanks in advance.
Updated 12-Apr-12 17:21pm

1 solution

I solved the problem myself, I put name to UserControl and in bining of button specified it by setting ElementName , and it worked. by yesturday when I did something similar like it, it throw error, but today it worked
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900