Click here to Skip to main content
16,017,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm trying to make a funny comic/meme app like funnyjunk.com, which contains a dislike and a like buttons on every comic, like this : http://s9.postimg.org/ikiyo7iy7/funnyjunk.png

My problem is: I can't get access to code the dislike/like buttons inside the DataTemplate from code behind. Is there any way to access buttons inside DataTemplate?

I'm using ListView in this case, and here is how my DataTemplate looks like :

XML
<stackpanel horizontalalignment="Center" verticalalignment="Center" orientation="Vertical">
            <stackpanel>
                <Image Source="{Binding Image}" Height="600" Width="800" Stretch="UniformToFill"/>    
            </stackpanel>
            <stackpanel horizontalalignment="Center" orientation="Horizontal">
                <Button x:Name="blike" Content="L" FontSize="70" HorizontalAlignment="Center" VerticalAlignment="Center" Click="blike_Click"/>
                <textblock x:name="tblrate" text="0" fontsize="70" horizontalalignment="Center" verticalalignment="Center" xmlns:x="#unknown" />
                <Button x:Name="bdislike" Content="D" FontSize="70" HorizontalAlignment="Center" VerticalAlignment="Center" Click="bdislike_Click"/>
            </stackpanel>
</stackpanel>


and Here is the codes for the buttons:
C#
private void blike_Click(object sender, RoutedEventArgs e)
{
    int rate = Convert.ToInt16(tblrate.Text);
    rate += 1;
    tblrate.Text = Convert.ToString(rate);
}

private void bdislike_Click(object sender, RoutedEventArgs e)
{
    int rate = Convert.ToInt16(tblrate.Text);
    rate -= 1;
    tblrate.Text = Convert.ToString(rate);
}


Here's my DataContext (I named it "DataSource"):

C#
public class DataSource
        {
            public int ID { get; set; }
            public string Title { get; set; }
            public string Image { get; set; }
            public DataSource(int _ID, string _Image, string _Title)
            {
                ID = _ID;
                Image = _Image;
                Title = _Title;
            }
        }

    public class DataFill
        {
            public List<DataSource> Comics= new List<DataSource>();
            public void MainPageComics()
            {
                Comics.Add(new DataSource(1, "/Assets/Comic1.jpg", "Jokur and Botmon"));
                Comics.Add(new DataSource(2, "/Assets/Comic2.jpg", "Jokur and Botmon2"));
            }
    }


What I'm trying to achieve is:

1. The dislike/like buttons to work in EACH and every comic that is BOUND inside the ListView, so it will change the value of the dislike/like RATE of the comic.

What I have tried:

1. Jerry Nixon's tutorial (it only works for textbox not textblock, i don't understand why)

2. I'm still trying to understand to use the Command property, which is very complex.
still looking for tutorials to use ICommands, if you have a tutorial video that would be very helpful.

I'm working on a METRO app btw, thank you in advance.
Posted
Updated 7-Aug-14 21:06pm
v3

You Can Add One More Property in your DataSource for DisLike/Like and using Binding You can set this property to your buttons. This way you will be able to User dislike/Like buttons for
each and every comic value that you have.

Hope this Will Work. Happy Coding.
 
Share this answer
 
Yes your understanding is correct. We can not access DataTemplate controls in code behind. So for this you have to use ICommand interface inside your ViewModel. So it is clear that you should be use MVVM pattern. And important is that use of Command property is not a complex task.

Can you please refer this links.

1) Passing Command Parameter for Buttons within an ItemTemplate using MVVM Pattern in a WPF Application[^]

2)http://www.c-sharpcorner.com/UploadFile/e06010/wpf-icommand-in-mvvm/[^]

I can explain it here also, but if you read articles already written by some one then it is better for your MVVM understanding.
 
Share this answer
 
Comments
OneOne Six 8-Aug-14 3:01am    
Sorry i forgot to mention that i'm working on a METRO App.. and i have tried your 1st link which is a Windows 8 program and i get this error ''The name 'CommandManager' does not exist in the current context'', i have googled this error [1] and i found that CommandManager doesn't work in Metro App (i have added reference System.Windows.Input).
Do you have another solution/different approach for this ''simple'' problem that i have??

it's just giving a command to a button and change the text of a textblock inside datatemplate, it looks and sounds so easy, but why is it so complicated implementing it in Metro App? Thank you.


[1] http://social.msdn.microsoft.com/Forums/windowsapps/en-US/6c2bd4b7-4600-4068-a121-9571920f9897/button-event-added-to-standardstylesxaml-not-firing-in-grid-application?forum=winappswithcsharp
Punamchand Dhuppad 8-Aug-14 4:29am    
I got the solution for this on MSDN refer this link and example this will solved your problem. This is explain you "How to bind Command to button in DataTemplate in Windows Store app". Download this example and us its Command folder classes.

http://code.msdn.microsoft.com/windowsapps/Command-binding-inside-3cef5eea

Hope this will help you.

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