【问题标题】:Telerik grid selection with a Button - MVVMTelerik 使用按钮选择网格 - MVVM
【发布时间】:2013-09-10 10:20:01
【问题描述】:

我有一个 Telerik RadGridView,它有几行和几列。其中一列包含一个按钮,它调用我的 ViewModel 的命令。

这是我的专栏描述:

<telerikGrid:RadGridView.Columns>

   <telerikGrid:GridViewColumn Width="32" IsVisible="{Binding IsAvailable}">
      <telerikGrid:GridViewColumn.CellTemplate>
          <DataTemplate> 
              <telerik:RadButton Width="24" Height="24" x:Name="AddItemToList"                                                    
             Command="{Binding AddItemToListCommand, RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType={x:Type UserControl}}}">

                  <Image Stretch="Uniform" Source="Images/add.png" HorizontalAlignment="Center" />
               </telerik:RadButton>
            </DataTemplate>
        </telerikGrid:GridViewColumn.CellTemplate>
      </telerikGrid:GridViewColumn>

      <telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
      <telerikGrid:GridViewDataColumn DataMemberBinding="{Binding ConName}"/>
      <telerikGrid:GridViewDataColumn DataMemberBinding="{Binding DisName}"/>

</telerikGrid:RadGridView.Columns>

When a row is selected and I click on the button, the good values are sent to the ViewModel.但是,我的问题是,如果我单击另一行的按钮(不是我选择的行),它不会选择该行,因此向 VM 发送错误的值(发送所选行的值)!

如何强制行选择以选择包含我正在单击的按钮的行? 它实际上应该在我的XAML 中完成,在我的命令执行之前。

希望它足够清楚。我希望将焦点设置在包含我正在单击的按钮的行上。

【问题讨论】:

  • 为了简化,我想在执行命令之前选择按钮单击的行。

标签: c# wpf xaml mvvm telerik


【解决方案1】:

您可以将 Selecteditem 作为 AddItemToListCommand 的 CommandParameter 发送 CommandParamter="{Binding}"。因此,您将获得行数据上下文,即单击按钮的项目。

谢谢

【讨论】:

  • 我只想在命令执行之前选择按钮单击时的行。
  • 兄弟你为什么想要它......你选择的项目是否绑定到任何视图模型属性?如果是,那么按照我建议的方法,您可以在执行任何操作之前在 CommandHandler 中设置它,因为它将是双向绑定,它将反映在 UI 上并且应该显示为选中。
  • 即使使用 CommandParamter="{Binding}",它也没有向我发送正确的上下文,即按钮的行。它让我回到了有焦点的那一行。通过点击按钮,'SelectedRow' 不会改变!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多