如何让巴顿在视图模型的内容? [英] How to get the Content of Button in ViewModel?

查看:102
本文介绍了如何让巴顿在视图模型的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令一个按钮设置视图模型。巴顿在运行时用值填充。
任何人可以帮助我得到的视图模型按钮的内容?

I have a button with Command set to viewModel. Button at runtime populating with values. Can anybody help me to get the button Content in the viewModel?

4个按钮在运行时与4组不同的值,总部设在点击该按钮,我会采取行动。说了4个按钮第二个按钮是点击那么我将减去分数100。如果被点击4按钮,然后我会在scrore加40。这100和40是按钮的内容。

4 buttons with 4 different set of values at runtime, and based in the button click I will take action. Say out of 4 buttons 2nd button is click then I will minus 100 from score. and if 4th Button is clicked then I will add 40 in the scrore. This 100 and 40 is the buttons content.

<Button Height="110" Width="230" Content="{Binding Path=ListOfValues[1]}" />
<Button Height="110" Width="230" Content="{Binding Path=ListOfValues[2]}" />
<Button Height="110" Width="230" Content="{Binding Path=ListOfValues[3]}" />
<Button Height="110" Width="230" Content="{Binding Path=ListOfValues[4]}" />

任何人可以帮助我在视图模型来获得在运行时的按钮值?

Can anybody help me to get the buttons values at runtime at viewModel?

感谢

推荐答案

如果这是WPF? - 做这样的事情帮助

If this is for WPF - does something like this help?

的XAML

<Window x:Class="WpfCommandTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.Resources>
        <RoutedUICommand x:Key="DoSomethingCommand" Text="Do Something" />
    </Grid.Resources>
    <Grid.CommandBindings>
        <CommandBinding Command="{StaticResource DoSomethingCommand}" Executed="OnDoSomethingExecuted" />
    </Grid.CommandBindings>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <!-- Bind the commandParameter to the button content -->
    <Button Grid.Row="0" Content="Button1" Command="{StaticResource DoSomethingCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
    <Button Grid.Row="1" Content="Button2" Command="{StaticResource DoSomethingCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
    <Button Grid.Row="2" Content="Button3" Command="{StaticResource DoSomethingCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
</Grid>

背后code:

namespace WpfCommandTest

{
使用System.Windows;
使用System.Windows.Input;

{ using System.Windows; using System.Windows.Input;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }


    private void OnDoSomethingExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        //Look at e.Parameter   <---!!!This line gives you the value of button content
    }
}

}

的想法是使用命令参数...

The idea is to use the command parameter...

这篇关于如何让巴顿在视图模型的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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