繁体   English   中英

从 DataGrid 中取消的命令参数

[英]Command parameter to void from DataGrid

我有一个包含几列的数据网格。 其中之一是idObiektu。 我想将 idObiektu(来自所选行)作为命令的参数传递

在视图中我有

<Button Content="Usun" Command="{Binding UsunCommand}"  CommandParameter="{Binding idObiektu}" />

<DataGrid.Columns>
                <DataGridTextColumn Header="Identyfikator" Binding="{Binding idObiektu}"/>
                    <DataGridTextColumn Header="Nazwa obiektu" Binding="{Binding Nazwa}" />
                <DataGridTextColumn Header="Adres" Binding="{Binding Adres}"/>
                    <DataGridTextColumn Header="Pojemność obiektu" Binding="{Binding Pojemnosc}" />
                    <DataGridTextColumn Header="Osoba kontakotwa" Binding="{Binding OsobaKontaktowa}"/>
                    <DataGridTextColumn Header="Numer telefonu" Binding="{Binding Telefon}"/>
                    <DataGridTextColumn Header="Adres email" Binding="{Binding Email}"/>
                    <DataGridTextColumn Header="Notatki" Binding="{Binding Notatki}"/>
</DataGrid.Columns>

在视图模型中我有

public string idObiektu
        {
            get
            {
                return _idObiektu;
            }
            set
            {
                if (_idObiektu != value)
                {
                    _idObiektu = value;
                    OnPropertyChanged(() => idObiektu);
                }

            }
        }

public ICommand UsunCommand { get { return new RelayCommand<string>(OnEdit); } }

        private void OnEdit(string itemToEdit)
        {

                int idObiektu = Int32.Parse(itemToEdit);
                atmaEntites.Database.ExecuteSqlCommand("UPDATE Obiekty SET stan = '2' WHERE idObiektu = " + idObiektu + ";");
        }

我有问题,因为每次itemToEdit为空

我尝试将 VievModel 更改为此,并且测试字符串 o 为 NULL

public ICommand UsunCommand { get { return new RelayCommand<object>(OnEdit); } }

        private void OnEdit(object itemToEdit)
        {
            string o = itemToEdit.ToString();
            if (itemToEdit is null)
            {
                string idObiektu = itemToEdit.ToString(); ;
                atmaEntites.Database.ExecuteSqlCommand("UPDATE Obiekty SET stan = '2' WHERE idObiektu = " + idObiektu + ";");
            }
            else
            {
                string idObiektu = itemToEdit.ToString(); ;
                atmaEntites.Database.ExecuteSqlCommand("UPDATE Obiekty SET stan = '2' WHERE idObiektu = " + idObiektu + ";");
            }
        }

在视图中,我将 CommandParameter Binding 更改为

<Button Content="Usun" Command="{Binding UsunCommand}"
        CommandParameter="{Binding Path=SelectedItem, ElementName=dataGrid}"/>

在视图模型中,我将对象类型更改为 DataGrid 中的项目类型

public ICommand UsunCommand { get { return new RelayCommand<ObiektyForAllViews>(OnEdit); } }

private void OnEdit(ObiektyForAllViews itemToEdit)
{
    int idObiektu = itemToEdit.idObiektu;
    atmaEntites.Database.ExecuteSqlCommand("UPDATE Obiekty SET stan = '2' WHERE idObiektu = " + idObiektu + ";");
}

新答案......在重新阅读帖子但仍然没有任何进展后,我发现了什么。 您提到您想要选定行中的 ID 值,该值以前并未触发我的思维定势。 这是我相信你拥有的和需要的。

为了让您的记录列表显示在您的屏幕上,您可能有类似的东西

public List<YourRecordDataClassStructure> MyListOfNames {get; set;}

然后,在表单中的某处,填充从数据库查询的记录列表。

由于您的帖子没有显示

<DataGrid . . . >

视图的组成部分,我只能假设它类似于

<DataGrid ItemsSource={Binding MyListOfNames} >

下一步...在您的视图模型上创建一个新的可绑定属性来表示要绑定到从网格中选择的“SelectedItem”的数据结构的单个实例...类似

public YourRecordDataClassStructure JustOneItem {get; set;}

并将您的 DataGrid 绑定更新为

<DataGrid ItemsSource={Binding MyListOfNames} 
      SelectedItem={Binding JustOneItem} >

所以现在你的表单有一个绑定到你的记录列表的网格。 它还会更新,当从数据网格中选择任何记录时,将当前记录的副本放入视图模型的“JustOneItem”属性中。

现在,您可以忽略参数和对象类型作为字符串,因为视图模型实际上拥有整个记录,并且可以通过更新方法主体从中获取任何/所有值

private void OnEdit(string itemToEdit)
{
   // confirm the selected object from the DataGrid was bound and actually selected.
   // if not, get out.
   if (JustOneItem== null || JustOneItem.MyIdColumn == 0)
      return;

   // we now have the entire object, get the ID column directly from 
   // the entire record displayed
   var idObiektu = JustOneItem.idObiektu;

   atmaEntites.Database.ExecuteSqlCommand("UPDATE Obiekty SET stan = '2' WHERE idObiektu = " + idObiektu + ";");
}

所以,按钮中继命令的第一步,我明确地预先检查是否有从网格中选择的记录。 如果什么都没有,请退出...此外,如果记录的 ID 值为 0,则忽略,因为如果没有要更新的 ID,您甚至可能不会在列表中包含任何记录。

HTH

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM