根据表格(DataGrid)的选定行设置按钮(ICommand)的操作

问题描述 投票:0回答:1
wpf datagrid inotifypropertychanged relaycommand
1个回答
0
投票

首先,有一个重要的说明。您传递 Window 命令参数

CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
。 由于此命令是一个 ViewModel 实体,并且根据 MVVM 规则,禁止将任何 UI 元素传递给 ViewModel,那么您的实现实际上完全破坏了 MVVM 合规性。

关于你问题的本质。
您的任务的可能实现选项之一的最简化示例。

namespace Core2024.SO.Zhan.question79147702
{
    public class SomeTextItem
    {
        private string _title = string.Empty;
        private string _text = string.Empty;
        private string _description = string.Empty;

        public string Title { get => _title; set => _title = value ?? string.Empty; }
        public string Text { get => _text; set => _text = value ?? string.Empty; }
        public string Description { get => _description; set => _description = value ?? string.Empty; }
    }
}
using Simplified; // This is the space with my implementation of INotifyPropertyChanged and ICommand interfaces. You can use your own implementations instead.
using System.Collections.ObjectModel;

namespace Core2024.SO.Zhan.question79147702
{
    public class SomeTextViewModel
    {
        private const string data = "After randomly selecting a row in each of the 2 tables \"DataGridRelatedPersonalTaskClass\" and \"DataGridDeletedPersonalTaskClass\", the \"RemoveTaskClassCommand\" button will never be able to change the value of the \"set\" property \"SelectedTaskClass\". Thus, if you click on it, the next highlighted task in the \"DataGridRelatedPersonalTaskClass\" will be deleted. Over time, I realized that if I highlighted each question in the tables, then the \"SelectedItem\" properties of each \"DataGrid\" continue to be active, but my \"SelectedTaskClass\" property is not always updated.Although the \"IsMouseOver\" property always correctly displays the row in which table I highlighted last.";
        private static IEnumerable<SomeTextItem> GetItems()
        {
            string[] split = data.Split();
            for (int i = 2; i < split.Length; i+=3)
            {
                yield return new SomeTextItem()
                {
                    Title = split[i-2],
                    Text = split[i-1],
                    Description = split[i]
                };
            }
        }
        public SomeTextViewModel()
        {
            TextItems = new ObservableCollection<SomeTextItem>(GetItems());
            RemoveTextItem = new RelayCommand<SomeTextItem>
            (
                item => TextItems.Remove(item),
                item => TextItems.Contains(item)
            );
        }

        public ObservableCollection<SomeTextItem> TextItems { get; }

        public RelayCommand RemoveTextItem { get; }
    }
}
<Window x:Class="Core2024.SO.Zhan.question79147702.SomeTextWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Core2024.SO.Zhan.question79147702"
        mc:Ignorable="d"
        Title="SomeTextWindow" Height="450" Width="800"
        DataContext="{DynamicResource vm}">
    <Window.Resources>
        <local:SomeTextViewModel x:Key="vm"/>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{Binding TextItems}">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate DataType="{x:Type local:SomeTextItem}">
                            <Button Content="Remove Item"
                                    CommandParameter="{Binding Mode=OneWay}"
                                    Command="{Binding RemoveTextItem, Mode=OneWay, Source={StaticResource vm}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
© www.soinside.com 2019 - 2024. All rights reserved.