如何解决无法将DataTemplate中的按钮绑定到RelayCommand
我有一个显示动态按钮列表的视图(显示为文本。)用户已经期望文本是“可选的”。但这不重要。
我试图将按钮绑定到RelayCommand,但是当我测试时,单击一行文本不会导致绑定命令被执行。我不确定我缺少什么。这是我第一次尝试使用带DataTemplate的ItemsControl。我想念什么?这是xaml视图:
<Window x:Class="MyCode.Correction.CorrectionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ls="clr-namespace:MyCode.Correction"
DataContext="{Binding Source={StaticResource Locator},Path=Correctionviewmodel,UpdateSourceTrigger=PropertyChanged}"
mc:Ignorable="d"
Width="270"
Height="300"
ResizeMode="noresize"
Title="Correction menu"
Topmost="True"
WindowStartupLocation="CenterOwner"
Icon="/MyApp;component/Images/cc.ico"
AutomationProperties.AutomationId="CorrectionWindow">
<Grid Margin="0,10,-6,9" RenderTransformOrigin="0.264,0.344">
<Grid.RowDeFinitions>
<RowDeFinition Height="114*" />
</Grid.RowDeFinitions>
<ItemsControl ItemsSource="{Binding Correctionoptions}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}"
Command="{Binding CorrectionCommand,Mode=OneTime}"
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Margin="20,5,0"
FontSize="15">
<Button.Template>
<ControlTemplate targettype="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
这是视图模型:
namespace MyCode.Correction
{
public class Correctionviewmodel : DialogviewmodelBase
{
private readonly IDialogService _dialogService;
private readonly ILogger Logger;
public Correctionviewmodel(IDialogService dialogService)
{
Logger = LoggerFactory.GetLoggerInstance(typeof(Correctionviewmodel));
CorrectionCommand = new RelayCommand<object>((s) => OnCorrectionClicked(s));
_dialogService = dialogService;
}
public RelayCommand<object> CorrectionCommand { get; set; }
private ObservableCollection<string> _correctionoptions;
public ObservableCollection<string> Correctionoptions
{
get
{
return _correctionoptions;
}
set
{
Set(() => Correctionoptions,ref _correctionoptions,value);
}
}
private void OnCorrectionClicked(object selectedCorrection)
{
UserDialogResult = (string)selectedCorrection;
CloseAction();
}
}
}
解决方法
您的命令和参数绑定错误。
-
Command
绑定必须指向父级ItemsControl
的数据上下文,父级CorrectionViewModel
包含CorrectionCommand
。这是通过RelativeSource
完成的。 -
CommandParameter
是当前数据上下文本身(单击的更正选项string
)。与TemplatedParent
相同的RelativeSource
仅用于控制模板,而不用于数据模板。
它应该像这样工作。
<ItemsControl ItemsSource="{Binding CorrectionOptions}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}"
Command="{Binding DataContext.CorrectionCommand,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
CommandParameter="{Binding}"
Margin="20,5,0"
FontSize="15">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。