如何在c#XAML中访问DataTemplate中的控件(按钮) [英] How to access controls (buttons) inside DataTemplate in c# XAML

查看:48
本文介绍了如何在c#XAML中访问DataTemplate中的控件(按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个有趣的漫画/ meme应用程序,如funnyjunk.com,其中包含每个漫画中的不喜欢和类似按钮,如下所示:http://s9.postimg.org/ikiyo7iy7/funnyjunk.png



我的问题是:我无法从代码隐藏的代码中访问DataTemplate中的不喜欢/类似按钮。有没有办法访问DataTemplate中的按钮?



我在这种情况下使用ListView,这就是我的DataTemplate的样子:



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 :

<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:

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);
}





这是我的DataContext(我把它命名为DataSource):





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

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"));
            }
    }





我想要达到的目标是:



1.在EACH中工作的不喜欢/喜欢的按钮以及ListView中BOUND的每个漫画,所以它会改变漫画中不喜欢/喜欢的价值。



我尝试了什么:



1. Jerry Nixon的教程(它只适用于文本框而不是文本块,我不是理解为什么)



2.我仍然试图理解使用Command属性,这非常复杂。

仍在寻找如果您有一个非常有用的教程视频,可以使用ICommands教程。



我正在使用METRO应用程序顺便说一下,谢谢你提前。



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.

推荐答案

您可以在数据源中为DisLike / Like添加一个属性并使用绑定您可以将此属性设置为按钮。通过这种方式,您可以使用用户不喜欢/喜欢的按钮来获得每个漫画价值。



希望这会起作用。快乐编码。
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.


是的,您的理解是正确的。我们无法在后面的代码中访问 DataTemplate 控件。因此,您必须在 ViewModel 中使用 ICommand 界面。所以很明显你应该使用MVVM模式。并且重要的是使用 Command 属性并不是一项复杂的任务。



您能否请参考此链接。



1) ^ ]



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



我也可以在这里解释一下,但是如果你读过一些已经写好的文章那么你的 MVVM 理解。
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.


这篇关于如何在c#XAML中访问DataTemplate中的控件(按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆