【问题标题】:Get the row details when a control in data template is selected选择数据模板中的控件时获取行详细信息
【发布时间】:2021-02-04 17:31:23
【问题描述】:

我正在构建一个 WPF 应用程序并尝试尽可能地坚持 MVVM 模式。我有一个列表框,里面有一个数据模板,其中包含TextBlockButton。如果单击数据模板中的按钮,它不会选择整行,所以我不知道它属于哪一行。我想抓取整个对象并将其绑定到视图模型中的属性。我可以得到一些帮助或解决方法吗?请坚持 mvvm 模式。

带有项目模板的列表框

<telerik:RadListBox Width="200" Height="150" HorizontalAlignment="Left" Margin="10" ItemsSource="{Binding ListOfSupplierInvoices}"  
                    SelectedItem="{Binding SelectedSupplierInvoice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
   <telerik:RadListBox.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" >
            <TextBlock Text="{Binding InvoiceNumber}" HorizontalAlignment="Left" Margin="5" ></TextBlock>
            <telerik:RadButton  Height="20"  >
               <telerik:RadButton.Content>
                  <Image Source="/ImageResources/Misc/delete.png" Stretch="Fill" />
               </telerik:RadButton.Content>
            </telerik:RadButton>
         </StackPanel>
      </DataTemplate>
   </telerik:RadListBox.ItemTemplate>
</telerik:RadListBox>

它在视图中的外观:

【问题讨论】:

  • Telerik RadButton 是否包含 Command 依赖属性?如果是这样,那么您可以创建一个从您的视图模型运行的命令绑定,并且当该方法运行时,只需更新您的SelectedSupplierInvoice 视图模型属性。您在 xaml 中将其设置为 TwoWay,因此它应该更新 RadListBox 的 SelectedItem。这将确保维护 MVVM。

标签: c# wpf mvvm telerik datatemplate


【解决方案1】:

据我了解您的代码,按钮对应于删除命令,这意味着您要删除与按钮关联的项目。在这种情况下,选择可能不需要更改,您只需将当前项目传递给删除命令即可。

Delete 命令添加到您的视图模型,如下所示:

public class MyViewModel : ViewModelBase
{
   public MyViewModel()
   {
      Delete = new DelegateCommand(ExecuteDelete, CanExecuteDelete);

      // ...other code.
   }

   public ICommand Delete { get; }

   private void ExecuteDelete(object obj)
   {
      var invoiceItem = (InvoiceItem) obj;

      // Use this only if you need the item to be selected.
      // SelectedSupplierInvoice = invoiceItem;

      // ...your delete logic.
   }

   private bool CanExecuteDelete(object obj)
   {
      // ...your can execute delete logic.
   }

   // ...other code.
}

注意我引入InvoiceItem作为项目类型,因为我不知道你的项目类型,简单地适应它。 Delete 命令获取作为参数传递的当前项目。如果您始终可以删除该项目,则无需选择它,因为它随后就消失了。

否则,取消注释该行,以便将 SelectedSupplierInvoice 设置为将通过双向绑定自动更新用户界面的项目,如果您已正确实现 INotifyPropertyChanged 或派生自 ViewModelBase,它公开了 @987654330 @ 或OnPropertyChanged 方法,例如:

private InvoiceItem _selectedSupplierInvoice;
public InvoiceItem SelectedSupplierInvoice
{
   get => _selectedSupplierInvoice;
   set
   {
      if (_selectedSupplierInvoice == value)
         return;

      _selectedSupplierInvoice = value;
      RaisePropertyChanged();
   }
}

在您的 XAML 中,将按钮连接到 RadListBoxDataContext 上的 Delete 命令。

<telerik:RadButton  Height="20"
                    Command="{Binding DataContext.Delete, RelativeSource={RelativeSource AncestorType={x:Type telerik:RadListBox}}}"
                    CommandParameter="{Binding}">
   <telerik:RadButton.Content>
      <Image Source="/ImageResources/Misc/delete.png" Stretch="Fill" />
   </telerik:RadButton.Content>
</telerik:RadButton>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2012-01-06
    • 2016-02-03
    相关资源
    最近更新 更多