简体   繁体   English

如何在ViewModel中获取Button的内容?

[英]How to get the Content of Button in ViewModel?

I have a button with Command set to viewModel. 我有一个命令设置为viewModel的按钮。 Button at runtime populating with values. 运行时填充值的按钮。 Can anybody help me to get the button Content in the viewModel? 任何人都可以帮我在viewModel中获取按钮内容吗?

4 buttons with 4 different set of values at runtime, and based in the button click I will take action. 4个按钮在运行时具有4个不同的值集,并且基于按钮单击我将采取行动。 Say out of 4 buttons 2nd button is click then I will minus 100 from score. 说出4个按钮第二个按钮是点击然后我将减去100分。 and if 4th Button is clicked then I will add 40 in the scrore. 如果单击第4个按钮,我将在scrore中添加40。 This 100 and 40 is the buttons content. 这100和40是按钮内容。

<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? 任何人都可以帮我在viewModel运行时获取按钮值吗?

Thanks 谢谢

If this is for WPF - does something like this help? 如果这是WPF - 这样做有帮助吗?

Xaml 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 behind: 代码背后:

namespace WpfCommandTest

{ using System.Windows; {using System.Windows; using System.Windows.Input; 使用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... 想法是使用命令参数...

在按钮的命令绑定时,提供命令参数作为按钮的内容

CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM