2

我正在使用 System.Windows.interactivity.dll 通过以下方式在我的 ViewModel 中获取鼠标事件。

 <ListBox x:Name="listBox" ItemsSource="{Binding HeaderList}" DisplayMemberPath="Text" Width="Auto"   Margin="0,0,0,300" Height="Auto"  >
    <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonUp">
                <i:InvokeCommandAction Command="{Binding MouseLeftButtonUpCommand}" 
                         CommandParameter="{Binding SelectedItem, ElementName=listBox}"/>
        </i:EventTrigger>

        </i:Interaction.Triggers>
</ListBox>

在 ViewModel 中。

        public class Headers
    {
        public Headers()
        {
            IsSelected = false;
        }

        public string Text { get; set; }
        public ListBox Control { get; set; }
        public bool IsSelected { get; set; }
    }
   public ObservableCollection<Headers> HeaderList
    {
        get { return _headerList; }
        set
        {
            _headerList = value;
            base.OnPropertyChanged("HeaderList");
        }

    }
 public ICommand MouseLeftButtonUpCommand { get; set; }
 public DesignTemplateViewModel()
    {
        string file = SessionHelper.FilePath;
        List<string> columns = new List<string>();


        if (!string.IsNullOrEmpty(file))
        {
            ExcelHelper Excel = new ExcelHelper(file);
            columns = Excel.GetHeader();
        }
        else
        {
            columns.Add("Name");
            columns.Add("FatherName");
            columns.Add("MotherName");
            columns.Add("Class");
            columns.Add("RollNo");
            columns.Add("ModeOfTransport");
            columns.Add("Phone");
            columns.Add("Mobile");
        }
        HeaderList = new ObservableCollection<Headers>();

        foreach (string column in columns)
        {
            HeaderList.Add(new Headers
            {
                Text = column,
            });
        }

        MouseLeftButtonUpCommand = new RelayCommand((item) => OnMouseLeftButtonUp((Headers)item));
    }
  private void OnMouseLeftButtonUp(Headers sender)
    {
        ListBox control = sender.Control as ListBox;
        DragDrop.DoDragDrop(control, sender.Text, DragDropEffects.Copy);
    }

所以在这里我需要传递多个对象,例如生成此事件的控件、鼠标相关属性等。现在我正在传递单个参数,这段代码工作正常。所以我的问题是如何从 Xaml(View) 传递多个参数并在此 ViewModel 上访问它们。任何代码帮助?

4

2 回答 2

3

您可以尝试自定义Converter and MultiBinding

<CommandParameter>
      <MultiBinding Converter="{StaticResource CustomConverter}">
       <Binding ElementName=".." Path=".."/>
       <Binding ElementName=".." Path=".."/>
      </MultiBinding>
</CommandParameter>

转换器

class CustomConverter : IMultiValueConverter 
{
    public object Convert (object[] Values, Type Target_Type, object Parameter, CultureInfo culture) 
    {
        var findCommandParameters = new FindCommandParameters();
        findCommandParameters.Property1 = (string)values[0];
        findCommandParameters.Property1 = (string)values[1];
        return findCommandParameters;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter,   System.Globalization.CultureInfo culture)
    {
       throw new NotImplementedException();
    }
}

参数

public class FindCommandParameters
{
  public string Property1 { get; set; }
  public string Property2 { get; set; }
}
于 2012-09-26T12:46:57.247 回答
2

我同意阿吉拉斯的观点。这就是它的完成方式。我改进了 Aghilas 的代码,以澄清缺少的内容。请注意,“i:InvokeCommandAction.CommandParameter”必须放在 invokeCommandAction 声明中。

    <ListBox x:Name="listBox" ItemsSource="{Binding HeaderList}" DisplayMemberPath="Text" Width="Auto"   Margin="0,0,0,300" Height="Auto"  >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonUp">
                <i:InvokeCommandAction Command="{Binding MouseLeftButtonUpCommand}">
                    <i:InvokeCommandAction.CommandParameter>
                        <MultiBinding Converter="{StaticResource XAMLResourceAddConverter}">
                            <Binding ElementName="listBox" Path="SelectedItem"/>
                            <Binding ElementName="listBox" Path="SelectedItem"/>
                        </MultiBinding>
                    </i:InvokeCommandAction.CommandParameter>
                </i:InvokeCommandAction>
            </i:EventTrigger>

        </i:Interaction.Triggers>
    </ListBox>
于 2013-12-15T07:39:02.930 回答