简体   繁体   中英

How to set a default value which is bound to another value for the combobox in datagrid

I have a datagrid which has one combobox column.

-the itemsource of the datagrid will be bound to the data from the database through RIA WCF

-the itemsource of the combobox which is inside the datagrid will be bound to the ListFinancialAccountType property which is a collection of Financial Account Types (also retrieve through RIA WCF).

I can show the list of all of the available Account Type Name for the combobox. However, I can't set the default value for the combobox which should be matched with the value of each row in the datagrid

For example: the value of the combobox will tell us what type of the financial account is(Asset, income,..) . However, its real value in the row is an integer type(FinancialAccountTypeId) but i want it to be displayed as either "Asset", "Income",... So i need to look up on the Financial Account Type List(which will be retrieved through RIA WCF). The FinancialAccountType has FinancialAccountTypeId and AccountTypeName properties.

So on the rows of the datagrid, the combobox will display "Asset", "Income",.... instead of 1,2,... .

For my FinancialAccount: properties are

                      AccountDescription  

                      AccountNumber

                     FinancialAccountTypeId                  example: 1,2,...

                     FinancialAccountType                   --->navigation property

For my FinancialAccountType: properties are

                      AccountTypeName                       example: "asset", ...

                      FinancialAccountTypeId                example: 1, 2...

                        FinancialAccounts                    ---> navigation property

In my code behind, i also populate a list of Financial Account Types I think I must have a loading Issue here. Any input will be appreciated

Here is my Code Behind

 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;
        }
    }
}

Here is my 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM