如何设置绑定到datagrid中组合框的另一个值的默认值

时间:2010-08-17 19:51:35

标签: c# .net silverlight silverlight-4.0 silverlight-toolkit

我有一个数据网格,它有一个组合框列。

- datagrid的itemsource将通过RIA WCF绑定到数据库中的数据

- 数据网格内的组合框的项目源将绑定到ListFinancialAccountType属性,该属性是财务帐户类型的集合(也通过RIA WCF检索)。

我可以显示组合框的所有可用帐户类型名称列表。但是,我无法设置组合框的默认值,它应该与datagrid中每行的值匹配

例如:组合框的价值将告诉我们什么类型的金融账户(资产,收入,......)。但是,它在行中的实际值是一个整数类型(FinancialAccountTypeId),但我希望它显示为“资产”,“收入”,......所以我需要查看财务帐户类型列表(这将是通过RIA WCF检索)。 FinancialAccountType具有FinancialAccountTypeId和AccountTypeName属性。

所以在datagrid的行上,组合框将显示“Asset”,“Income”,....而不是1,2,....

对于我的FinancialAccount:属性

                      AccountDescription  

                      AccountNumber

                     FinancialAccountTypeId                  example: 1,2,...

                     FinancialAccountType                   --->navigation property

对于我的FinancialAccountType:属性

                      AccountTypeName                       example: "asset", ...

                      FinancialAccountTypeId                example: 1, 2...

                        FinancialAccounts                    ---> navigation property

在我的代码背后,我还填写了一个财务帐户类型列表 我想我必须在这里加载问题。任何输入将不胜感激

这是我的代码背后

 public partial class Financial_Accounts : Page
{
    public Financial_Accounts()
    {
        InitializeComponent();
    }

    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        FinancialAccountContext financial_ctx = new FinancialAccountContext();
        financial_ctx.Load(financial_ctx.GetFinancialAccountsQuery());
        financialAccountDataGrid.ItemsSource = financial_ctx.FinancialAccounts;

    }

}

public class Financial_Account_Types : ObservableCollection<FinancialAccountType>
{
    public EntitySet<FinancialAccountType> ListFinancialAccountType
    {
        get 
        {
            FinancialAccountContext financial_ctx = new FinancialAccountContext();
            financial_ctx.Load(financial_ctx.GetFinancialAccountTypesQuery());
            return financial_ctx.FinancialAccountTypes;
        }
    }
}

这是我的XAML

 <Grid.Resources>
        <financial:Financial_Account_Types x:Key="FinancialAccountTypes"/>
    </Grid.Resources>
    <sdk:DataGrid AutoGenerateColumns="False" Height="159" HorizontalAlignment="Left" Name="financialAccountDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="280">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn x:Name="accountDescriptionColumn" Binding="{Binding Path=AccountDescription}" Header="Account Name" Width="SizeToHeader" />
            <sdk:DataGridTextColumn x:Name="accountNumberColumn" Binding="{Binding Path=AccountNumber}" Header="Account Number" Width="SizeToHeader" />
            <sdk:DataGridTemplateColumn x:Name="financialAccountTypeIdColumn" Header="Account Type" Width="SizeToHeader">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox  ItemsSource="{Binding ListFinancialAccountType, Source={StaticResource FinancialAccountTypes},Mode=TwoWay}"
                                    SelectedValue="{Binding FinancialAccountTypeId, Mode=TwoWay}"

                                  DisplayMemberPath="AccountTypeName"
                                  SelectedValuePath="FinancialAccountTypeId"
                                  />

                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>

1 个答案:

答案 0 :(得分:0)

相关问题