如何通过 RelayCommand (mvvmlight) 获取 ViewModel 中按钮的内容
How can I get content of an button in my ViewModel via RelayCommand (mvvmlight)
我不知道如何获取按钮的内容(对于 examle,如果按钮具有内容 "press me",我希望它作为字符串出现在我的视图模型中)。
我已经尝试通过消息服务和命令参数发送它,但没有成功...
这是我的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>
我想要按钮的值(文本块的文本)-> "product" 在我的视图模型中作为字符串
希望你能帮助我,在此先感谢!!
编辑:
这是绑定到按钮的命令,我不知道如何实现 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;
}
}
您不必将 TextBlock
的 Text
作为命令参数传递,因为您绑定到 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>
我不知道如何获取按钮的内容(对于 examle,如果按钮具有内容 "press me",我希望它作为字符串出现在我的视图模型中)。
我已经尝试通过消息服务和命令参数发送它,但没有成功...
这是我的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>
我想要按钮的值(文本块的文本)-> "product" 在我的视图模型中作为字符串
希望你能帮助我,在此先感谢!!
编辑:
这是绑定到按钮的命令,我不知道如何实现 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;
}
}
您不必将 TextBlock
的 Text
作为命令参数传递,因为您绑定到 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>