如何在ItemsControl DataTemplate中使用ComboBox SelectedItem

时间:2014-08-14 18:56:49

标签: c# wpf mvvm combobox

我正在使用表单向订单添加商品。我对XAML的项目区域有什么:

<ItemsControl Name="ServiceItems" ItemsSource="{Binding ServiceOrderItems}" Width="737" Canvas.Left="0" Canvas.Top="193" Visibility="{Binding ShowServiceItems, Converter={StaticResource BooleanToVisibility}}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="140*" />
                    <ColumnDefinition Width="320*" />
                    <ColumnDefinition Width="54*" />
                    <ColumnDefinition Width="35*" />
                    <ColumnDefinition Width="47*" />
                    <ColumnDefinition Width="47*" />
                    <ColumnDefinition Width="47*" />
                    <ColumnDefinition Width="47*" />
                </Grid.ColumnDefinitions>

                <ComboBox ItemsSource="{Binding ElementName=ServiceItems, Path=DataContext.ItemsList}" SelectedItem="{Binding Path=Item, Mode=TwoWay}" IsTextSearchEnabled="True">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <i:InvokeCommandAction Command="{Binding ElementName=ServiceItems, Path=DataContext.CommandAddServiceItem}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ComboBox>
                <TextBox Text="{Binding Description, Mode=TwoWay}" Grid.Column="1" />
                <TextBox Text="{Binding Available, Mode=TwoWay}" Grid.Column="2" />
                <TextBox Text="{Binding Quantity, Mode=TwoWay}" Grid.Column="3" />
                <TextBox Text="{Binding Rate, Mode=TwoWay}" Grid.Column="4" />
                <TextBox Text="{Binding Amount, Mode=TwoWay}" Grid.Column="5" />
                <CheckBox IsChecked="{Binding Taxable, Mode=TwoWay}" Grid.Column="6" />
                <CheckBox IsChecked="{Binding Closed, Mode=TwoWay}" Grid.Column="7" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <ScrollViewer CanContentScroll="True">
                <ItemsPresenter/>
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

这是我的MVVM类:

namespace ServiceOrders
{
    public class SalesOrderItem
    {
        public Int64 ItemId { get; set; }
        public string Item { get; set; }
        public string Description { get; set; }
        public int Available { get; set; }
        public double Quantity { get; set; }
        public double Rate { get; set; }
        public double Amount { get; set; }
        public bool Taxable { get; set; }
        public bool Closed { get; set; }

        public override string ToString()
        {
            return this.Item;
        }
    }

    public ObservableCollection<ServiceOrders.SalesOrderItem> ItemsList { get; set; }
    public ObservableCollection<ServiceOrders.SalesOrderItem> ServiceOrderItems { get; set; }

    public SalesOrderControlViewModel()
    {
        ItemsList = new ObservableCollection<ServiceOrders.SalesOrderItem>();
        ServiceOrderItems = new ObservableCollection<ServiceOrders.SalesOrderItem>();
    }
}

除了ComboBox之外,我的所有绑定似乎都有效。我想从项目列表(ItemsList)中提取,但我想将SelectedItem添加到ServiceOrderItems。当我选择该项目时,它会添加它但只添加SalesOrderItem.Item并且不会添加描述,数量等。

我如何实现我想要做的事情?我尝试过以下方法:

<ComboBox ItemsSource="{Binding ElementName=ServiceItems, Path=DataContext.ItemsList}" SelectedItem="{Binding ElementName=ServiceItems, Path=DataContext.SelectedServiceItem, Mode=TwoWay}" IsTextSearchEnabled="True" DisplayMemberPath="Item">

但这只是将所有项目行ComboBoxes设置为相同的东西。我也试过SelectionChanged事件,然后尝试获取SelectedItem并将其添加到ServiceOrderItems

我看了一遍,无法找到符合我想要做的事情。

1 个答案:

答案 0 :(得分:0)

添加字符串是因为您已将 SelectedItem 与字符串值绑定,并且我怀疑您在命令中添加了所选项目的命令。

<ComboBox ItemsSource="{Binding ElementName=ServiceItems,
                                Path=DataContext.ItemsList}"
          SelectedItem="{Binding Path=Item, Mode=TwoWay}">

将SelectedItem绑定到SalesOrderItem类型的对象,称为SelectedSalesOrderItem ,以便您可以访问完整对象。

如果它只是在comboBox中显示Item,则不需要重写ToString()。您可以使用 DisplayMemberPath 属性在对象的UI上显示特定属性。

<ComboBox ItemsSource="{Binding ElementName=ServiceItems,
                                Path=DataContext.ItemsList}"
          SelectedItem="{Binding Path=SelectedSalesOrderItem, Mode=TwoWay}"
          DisplayMemberPath="Item">