簡體   English   中英

獲取觸發按鈕的 ObservableCollection 項

[英]Get item of ObservableCollection that triggers button

所以我已經在 c# 中研究智能家居實現了很長一段時間,但我不知道如何通知執行某些操作的命令,在這種情況下只是一個刪除按鈕作為示例,關於項目的觸發它的可觀察對象。 為了實現集合,我使用了一個模板,該模板還包含一個按鈕,以便集合的每個項目都有一個自己的按鈕。

ObservableCollection 與每個項目的刪除按鈕

因此,正如您在那里看到的,我為 ObservableCollection 中的每個項目都有一個刪除按鈕,如果我按下它,我希望刪除包含該按鈕的項目,但不使用 selectedItem 之類的東西,因為在這種情況下我需要選擇在使用按鈕之前先選擇項目,這不是我真正想要的,因為您可以在不選擇項目/行的情況下按下按鈕。 稍后按鈕將被替換為不同的功能,該功能需要知道按鈕屬於哪個項目。

xml代碼:

<Page x:Class="Smart_Home.switchPage"
        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:Smart_Home"
        mc:Ignorable="d"
        Title="switchPage" Height="490" Width="998" Background="#10636e" >
  <Page.Resources>
    <local:SwitchViewmodel x:Key="SwitchViewmodel" />
    <DataTemplate x:Key="UserTemplate">
      <StackPanel>
        <Grid>
          <Grid Width="970" Margin="0,0,0,0">
            <Grid HorizontalAlignment="Left" VerticalAlignment="Center" Width="300">
              <TextBlock Text="{Binding Path=schalterName}"/>
            </Grid>
            <Grid HorizontalAlignment="Center" Width="380">
              <Grid HorizontalAlignment="Left" VerticalAlignment="Center" Width="200">
                <TextBlock Text="{Binding Path=schalterRaum}"/>
              </Grid>
              <Grid HorizontalAlignment="Right" VerticalAlignment="Center" Width="30">
                <TextBlock Text="{Binding Path=schalterNummer}"/>
              </Grid>
            </Grid>
            <Grid HorizontalAlignment="Right" VerticalAlignment="Center" Width="60">
              <Button Content="Switch" Height="40" Command="{Binding RemoveSelectedSwitch, Source={StaticResource SwitchViewmodel}}" />
            </Grid>
          </Grid>
        </Grid>
      </StackPanel>
    </DataTemplate>
  </Page.Resources>

  <Grid x:Name="WindowSelection" HorizontalAlignment="Left" Height="490" Margin="0,0,0,0" VerticalAlignment="Top" Width="998">
    <Grid Height="400" VerticalAlignment="Top" Grid.ColumnSpan="2">
      <Grid x:Name="ListNameGrid" VerticalAlignment="Top" Height="30">
        <Grid HorizontalAlignment="Left" Margin="10,0,0,0">
          <Label Content="Name" FontSize="14" Foreground="White"/>
        </Grid>
        <Grid HorizontalAlignment="Center" Width="400">
          <Grid HorizontalAlignment="Left">
            <Label Content="Room" FontSize="14" Foreground="White"/>
          </Grid>
          <Grid HorizontalAlignment="Right">
            <Label Content="Number" FontSize="14" Foreground="White"/>
          </Grid>
        </Grid>
        <Grid HorizontalAlignment="Right" Margin="0,0,10,0">
          <Label Content="Status" FontSize="14" Foreground="White" />
        </Grid>
      </Grid>
      <Grid x:Name="ListGrid" VerticalAlignment="Top" Margin="0,30,0,0">
        <ListBox x:Name="Switches_lb" IsSynchronizedWithCurrentItem="true" FontSize="20" Foreground="White" ItemsSource="{Binding SwitchesList, Source={StaticResource SwitchViewmodel}}" ItemTemplate="{StaticResource UserTemplate}" Background="#10636e" BorderThickness="0" Margin="5,0,5,0"/>
      </Grid>
    </Grid>
  </Grid>
</Page>

視圖模型:

public class SwitchViewmodel
  {
    public int selectedCollection { get; }
    public ObservableCollection<SwitchView> SwitchesList { get; set; }
    public ICommand AddNewSwitch { get; set; }
    public ICommand RemoveSelectedSwitch { get; set; }


    public SwitchViewmodel()
    {
      AddNewSwitch = new Command(AddNewSwitchExecuteMethod, AddNewSwitchcanexecuteMethod);
      RemoveSelectedSwitch = new Command(RemoveSelectedSwitchExeCuteMethod, RemoveSelectedSwitchAddNewSwitchcanexecuteMethod);

      SwitchesList = new ObservableCollection<SwitchView>
      {
        new SwitchView() { schalterName = "Wandlampe", schalterRaum = "Wohnzimmer", schalterNummer = 1, schalterStatus = false },
        new SwitchView() { schalterName = "Deckenlampe", schalterRaum = "Küche", schalterNummer = 2, schalterStatus = false },
        new SwitchView() { schalterName = "Tischlampe", schalterRaum = "Wohnzimmer", schalterNummer = 39, schalterStatus = false },
      };

    }

    private bool RemoveSelectedSwitchAddNewSwitchcanexecuteMethod(object parameter)
    {
      return true;
    }

    private void RemoveSelectedSwitchExeCuteMethod(object parameter)
    {
      //In here remove method
    }

    private bool AddNewSwitchcanexecuteMethod(object parameter)
    {
      return true;
    }

    private void AddNewSwitchExecuteMethod(object parameter)
    {
      Console.WriteLine("Command test");
      Console.WriteLine("Count vorher: " + SwitchesList.Count());
      SwitchesList.Add(new SwitchView() { schalterName = "Wandlampe", schalterRaum = "Wohnzimmer", schalterNummer = 1, schalterStatus = false });
      Console.WriteLine("Count nachher: " + SwitchesList.Count());
    }
  }

收藏:

public class SwitchView
  {
    public string schalterName { get; set; }
    public string schalterRaum { get; set; }
    public int schalterNummer { get; set; }
    public Boolean schalterStatus { get; set; }
  }

命令:

  public class Command : ICommand
  {
    Action<object> executeMethod;
    Func<object, bool> canexecuteMethod;

    public Command(Action<object> executeMethod, Func<object, bool> canexecuteMethod)
    {
      this.executeMethod = executeMethod;
      this.canexecuteMethod = canexecuteMethod;
    }

    public bool CanExecute(object parameter)
    {
      return true;
    }

    public void Execute(object parameter)
    {
      executeMethod(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
    }
  }

因此,如果有人可以幫助我解決我的問題,那將非常有幫助,因為我在 c# wpf 中使用 MVVM 模式真的很陌生。

謝謝你,祝你有美好的一天

添加命令參數。

<Button Content="Switch" Height="40" 
        Command="{Binding RemoveSelectedSwitch, Source={StaticResource SwitchViewmodel}}"
        CommandParameter="{Binding}" />

並在命令方法中使用接收到的參數。

private void RemoveSelectedSwitchExeCuteMethod(object parameter)
{
     var item = parameter as SwitchView;
}

由於 Button 在 ListBox ItemTemplate 內, CommandParameter="{Binding}"將返回 ListBoxItem 的 DataContext - 這意味着相應的數據項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM