微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将DataTemplate中的按钮绑定到RelayCommand

如何解决无法将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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?