Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

silverlight - Bbinding combobox within dataform to view model property outside dataform's context

I have two properties in my view model:

//Relationship has property ReasonForEndingId
private Relationship editRelationship;
public Relationship EditRelationship
{
    get
    {
        return editRelationship;
    }

    set
    {
        if (editRelationship != value)
        {
            editRelationship = value;
            RaisePropertyChanged(EditRelationshipChangedEventArgs);
        }
    }
}

//ReasonForLeaving has properties Reason & Id
private IList<ReasonForLeaving> reasonsComboList { get; set; }
public IList<ReasonForLeaving> ReasonsComboList
{
    get
    {
        return reasonsComboList;
    }

    private set
    {
        if (reasonsComboList != value)
        {
            reasonsComboList = value;
            RaisePropertyChanged(ReasonsComboListChangedEventArgs);
        }
    }
}

In my xaml I have the following: (specifically note the binding on the dataform and combobox)

<toolkit:DataForm x:Name="EditForm" CurrentItem="{Binding EditRelationship, Mode=TwoWay}">
    <toolkit:DataForm.EditTemplate>
    <DataTemplate>
            <StackPanel>
                <toolkit:DataField>
                    <ComboBox x:Name="EndReasonCombo" ItemsSource="{Binding ReasonsComboList}" DisplayMemberPath="Reason" SelectedValuePath="Id" SelectedValue="{Binding ReasonForEndingId, Mode=TwoWay}"/>
                </toolkit:DataField>

So, I'm trying to bind to a list that exists in my viewmodel (the datacontext for the page). However, the DataForm's datacontext is EditRelationship. ReasonsComboList does not exist within EditRelationship.

How can I bind the combobox so that it will display the list of items available in ReasonsComboList?

Thanks for your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's what I did (tested and works):

Within a DataForm this won't work (because its a DataTemplate) :

<ComboBox MinWidth="150" DisplayMemberPath="Name" Name="cbCompanies"
          SelectedItem="{Binding TODOCompany,Mode=TwoWay}" 
          ItemsSource="{Binding ElementName=control, Path=ParentModel.Companies}" />

But you can do this instead:

<ComboBox MinWidth="150" DisplayMemberPath="Name" Name="cbCompanies"
          SelectedItem="{Binding TODOCompany,Mode=TwoWay}" 
          Loaded="cbCompanies_Loaded"/>

Code behind:

private void cbCompanies_Loaded(object sender, RoutedEventArgs e)
{
    // set combobox items source from wherever you want
    (sender as ComboBox).ItemsSource = ParentModel.Companies;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...