簡體   English   中英

如何使作為響應提供給Command的參數提供的對象(ViewList.SelectedItems)執行?

[英]How do I make the object(ViewList.SelectedItems) provided as a parameter to Command executed in response to an event?

這似乎是如此基礎,以至於我覺得在尋找解決方案時一定會錯過一些東西。

我有一個ViewModel ,它具有ObservableCollection <ExcelField>屬性,該屬性用作關聯View中 ListViewItemsSource

我已經添加在視圖一個EventToCommand項其訪問RelayCommand命令傳遞給在視圖模型的方法(ExcelListChanged)與被設置為在Viewlist產品 selectedItems屬性的參數。

在我的ViewModel中 ,想構造另一個包含已選擇項目的List 在調試器中,我可以檢查對象參數的內容,並驗證它是否確實包含我想要訪問的信息。 不幸的是,我無法找到使我能夠訪問參數對象中數據的鍵,我嘗試了強制轉換,分配,ChangeTo等。 具有未知轉換或類似內容的強制轉換和ChangeTo異常。 我以為由於該對象是ListView.SelectedItems (?),所以我可以將其轉換為某種風格,但是在ListView對象外部似乎不可行。

一個簡單的解決方案將是一個不錯的選擇,一個復雜的解決方案將是可以的,復雜的解決方案將是一個痛苦的解決方案。

感謝您的指導。

這是代碼片段

數據結構

public struct ExcelField
{
    private int index;
    public int Index
    {
        get { return index; }
        private set { index = value; }
    }
    private string fieldName;
    public string FieldName
    {
        get { return fieldName; }
        private set { fieldName = value;  }
    }

    public ExcelField(int ndx, string name)
    {
        index = ndx;
        fieldName = name;
    }
}

ViewModel中的片段

    ObservableCollection<ExcelField> fieldNames;
    public ObservableCollection<ExcelField> FieldNames
    {
        get
        {
            return fieldNames;
        }
        set
        {
            fieldNames = value;
            OnPropertyChanged("FieldNames");
        }
    }
       allListChanged = new RelayCommand(args => ExcelListChanged(args));
    RelayCommand allListChanged;
    public RelayCommand AllListChanged
    {
        get
        {
            return allListChanged;
        }
    }
    private void ExcelListChanged(object parameter)
    {
        var whata = parameter.GetType();
        return;
    }

最后是View件

<UserControl.Resources>
    <ViewModel:ExcelMapperViewModel x:Key="ExcelMapperViewModelDataSource" d:IsDataSource="True"/>
    <DataTemplate x:Key="ExcelFieldTemplate">
        <StackPanel>
            <TextBlock Text="{Binding FieldName, Mode=OneWay}"/>
            <TextBlock Text="{Binding Index, Mode=OneWay}"/>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>


    <ListView x:Name="AllFields" Grid.Column="1" Grid.Row="1" Grid.RowSpan="3"
        ItemsSource="{Binding FieldNames}"
        ItemTemplate="{StaticResource ExcelFieldTemplate}"
        >
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="IsSelected" Value="{Binding IsSelected}" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Index}" Header="Index"/>
                <GridViewColumn DisplayMemberBinding="{Binding FieldName}" Header="Field Name"/>
            </GridView>
        </ListView.View>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <Custom:EventToCommand
                    Command="{Binding AllListChanged, Mode=OneWay}"
                    CommandParameter="{Binding ElementName=AllFields, Path=SelectedItems}"
                />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListView>

我在ExcelListChanged上設置一個斷點並運行代碼。 當我在ListView中選擇項目時,將觸發ExcelListChanged並檢查參數對象 我能夠看到選定的條目數作為Count on參數和ExcelField項目數組,並具有結構內字段的適當信息。

那么,如何以編程方式訪問在調試器中看到的信息?

由於SelectedItems是一個IList http://msdn.microsoft.com/zh-cn/library/system.collections.ilist(v=vs.110).aspx ,因此可以簡單地對其進行迭代...

   private void ExcelListChanged(object parameter)
{
    IList iConverted= parameter as IList;
    if(iConverted!=null){
      foreach(YouKnowTheTypeOfElements theElement in iConverted) {
         doSomethingWith(theElement);
      }
    }
    return;
}

暫無
暫無

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

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