【问题标题】:How can I get content of an button in my ViewModel via RelayCommand (mvvmlight)如何通过 RelayCommand (mvvmlight) 获取 ViewModel 中按钮的内容
【发布时间】:2019-08-16 11:58:25
【问题描述】:

我不知道如何获取按钮的内容(例如,如果按钮具有“按我”的内容,我希望它作为字符串出现在我的视图模型上)。

我已经尝试通过消息服务和命令参数发送它,但它没有工作......

这是我的 xaml:

<Button x:Name="btn">
                            <Button.Content>
                                <StackPanel Orientation="Horizontal">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition></RowDefinition>
                                            <RowDefinition></RowDefinition>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="1" x:Name="product" Text="{Binding Artikel}" ></TextBlock>
                                    </Grid>
                                </StackPanel>
                            </Button.Content>
                        </Button>

我想要按钮的值(文本块的文本)-> 我的视图模型中的“产品”作为字符串

希望你能帮帮我,在此先感谢!!

编辑:

这是绑定到按钮的命令,我不知道如何实现 CanExecute 方法...

 RelayCommand sendDataBtn;
    public RelayCommand SendDataBtn
    {
        get
        {
            if (sendDataBtn == null)
            {
                sendDataBtn = new RelayCommand(() =>
                {
                    if (Globals.isLoggedIn == true) {
                      //do smth
                    }
                    else
                    {

                        var msg = new OpenOrCloseCameraPage() { Name = "open" };
                        Messenger.Default.Send<OpenOrCloseCameraPage>(msg);
                    }

                });
            }
            return sendDataBtn;
        }
    }

【问题讨论】:

  • "product" 不是文本块的文本,它是您分配给文本块的名称。文本绑定到属性 Artikel。你能清楚你想要发生什么吗?

标签: c# wpf mvvm mvvm-light


【解决方案1】:

您不必将TextBlockText 作为命令参数传递,因为您已绑定到Artikel,但如果您仍然想这样做,您可以绑定到TextBlock,例如这个:

<Button x:Name="btn" Command="{Binding Command}" CommandParameter="{Binding Text, ElementName=product}">
    <Button.Content>
        <StackPanel Orientation="Horizontal">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="1" x:Name="product" Text="{Binding Artikel}" ></TextBlock>
            </Grid>
        </StackPanel>
    </Button.Content>
</Button>

【讨论】:

  • 感谢您的回答!以及如何访问我的 VM 中的 CommandParameter。很抱歉问这样的问题,我的大脑不再工作了
  • @nairda-newb:这取决于您的ICommand 实施。 Execute 方法应该接受一个参数。如果您使用的是RelayCommand&lt;string&gt;,则该方法接受string
  • @nairda-newb:不客气。如果您还有其他问题,请提出新问题。
  • 嘿,我不明白 Execute 方法的意义是什么。我从来不需要使用它。而且我也从来不需要实现 ICommand(可能是因为我总是使用 MVVMLight 及其实现?!)我添加了一些代码来向您展示我如何处理命令。
  • @nairda-newb:有一个通用的RelayCommand&lt;T&gt; 类型,它接受Action&lt;T&gt;。非泛型 RelayCommand 不接受命令参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 2013-01-05
  • 2012-04-05
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
相关资源
最近更新 更多