DataGrid中的ComboBox没有选择的项目

时间:2014-03-07 01:40:11

标签: silverlight xaml mvvm

我有一个绑定到项目源的数据网格。在数据网格内部,我有一个组合框,其项目源在视图模型中。我为组合框设置了所选项目,该组合框来自数据网格的项目源,但未被选中。组合框有项目,但每行都没有选择相应的项目。

<navigation:Page.Resources>
    <VM:TransferStockroomGLViewModel x:Key="TransferStockroomGLViewModel" />        
</navigation:Page.Resources>

 <data:DataGrid x:Name="dgTransferStockroomGLDetails" AutoGenerateColumns="False" ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
                           VerticalScrollBarVisibility="Visible"  ItemsSource="{Binding StockroomTransferDetails}"
                           CanUserResizeColumns="False" VerticalAlignment="Top" RowBackground="White" AlternatingRowBackground="White" GridLinesVisibility="All" Height="400">
            <data:DataGrid.Columns>
                <data:DataGridTemplateColumn Header="From Stockroom" Width="200" CanUserReorder="True" CanUserSort="True" IsReadOnly="False">
                    <data:DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="prim:DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
                            <Setter Property="FontWeight" Value="Bold"/>
                        </Style>
                    </data:DataGridTemplateColumn.HeaderStyle>
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <!--<TextBox Text="{Binding From_Stkrm_Id}" Width="200" Height="30" />-->
                            <ComboBox Width="200" Height="30" ItemsSource="{Binding Source={StaticResource TransferStockroomGLViewModel},Path=WiingsStkrmList}"
                                      SelectedValuePath="From_Stkrm_Name" DisplayMemberPath="Name" />
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                </data:DataGridTemplateColumn>                  
            </data:DataGrid.Columns>
        </data:DataGrid>

查看型号:

 private ObservableCollection<BuyerWebService.Stockroom> wiingsStkrmList;
    public ObservableCollection<BuyerWebService.Stockroom> WiingsStkrmList
    {
        get 
        { 
            return wiingsStkrmList; 
        }
        set 
        { 
            wiingsStkrmList = value;
            SendChangedNotification("WiingsStkrmList");
        }
    }

    private ObservableCollection<BuyerWebService.StockroomTransfer> stockroomTransferdetails;
    public ObservableCollection<BuyerWebService.StockroomTransfer> StockroomTransferDetails
    {
        get
        {
            return stockroomTransferdetails;
        }
        set
        {
            stockroomTransferdetails = value;
            SendChangedNotification("StockroomTransferDetails");
        }
    }

public TransferStockroomGLViewModel()
    {
        app = (App)Application.Current;
        GetWiingsStockroomList();
        GetStockroomTransferGLDetails();         
    }

    public void GetStockroomTransferGLDetails()
    {
        try
        {
            bsc.GetStockroomTransferGLDetailsCompleted += new EventHandler<BuyerWebService.GetStockroomTransferGLDetailsCompletedEventArgs>(bsc_GetStockroomTransferGLDetailsCompleted);
            bsc.GetStockroomTransferGLDetailsAsync(app.LogonSiteID);
        }
        catch (Exception ex)
        {
            LogException.CatchException(ex);
        }
    }

    void bsc_GetStockroomTransferGLDetailsCompleted(object sender, BuyerWebService.GetStockroomTransferGLDetailsCompletedEventArgs e)
    {
        try
        {
            if (e.Result != null)
            {
                StockroomTransferDetails = e.Result;
            }
        }
        catch (Exception ex)
        {
            LogException.CatchException(ex);
        }
        finally
        {                
            bsc.GetStockroomTransferGLDetailsCompleted -= new EventHandler<BuyerWebService.GetStockroomTransferGLDetailsCompletedEventArgs>(bsc_GetStockroomTransferGLDetailsCompleted);
        }
    }

    public void GetWiingsStockroomList()
    {
        try
        {
            bsc.GetWiingsStockroomListCompleted += new EventHandler<BuyerWebService.GetWiingsStockroomListCompletedEventArgs>(bsc_GetWiingsStockroomListCompleted);
            bsc.GetWiingsStockroomListAsync(app.LogonSiteID);
        }
        catch (Exception ex)
        {
            LogException.CatchException(ex);
        }
    }

    void bsc_GetWiingsStockroomListCompleted(object sender, BuyerWebService.GetWiingsStockroomListCompletedEventArgs e)
    {
        try
        {
            if (e.Result != null)
            {                    
                WiingsStkrmList = e.Result;
            }
        }
        catch (Exception ex)
        {
            LogException.CatchException(ex);
        }
        finally
        {
            bsc.GetWiingsStockroomListCompleted -= new EventHandler<BuyerWebService.GetWiingsStockroomListCompletedEventArgs>(bsc_GetWiingsStockroomListCompleted);
        }
    }

我希望每个记录组合框都应该有所选项目,但它不会出现。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您希望SelectedValuePath确定所选的值,但事实并非如此。属性SelectedValue将执行此操作。 SelectedValuePath确定如何在代表该项目的每个组合框项目上查找属性,并且可以用于检查这是否与SelectedValue相同。

<强>解决方案: 我假设您的类型BuyerWebService.Stockroom具有字符串属性Name。 这意味着你的绑定看起来像这样:

<ComboBox
    ItemsSource="{Binding Source={StaticResource TransferStockroomGLViewModel}, Path=WiingsStkrmList}"
    SelectedValuePath="Name"
    SelectedValue="{Binding From_Stkrm_Name, Mode=TwoWay}"
    DisplayMemberPath="Name"
    Width="200" Height="30"/>