开发者

Setting multiple Datacontext

I am trying to find out how to set correctly multiple DataContexts in XAML page. I have a basic collection that I create in code behind and set ItemSource Binding og AutoCompleteBox to it. At the same time, I have another datacontext to set labelsDataSource inside the grid. If I set this datacontext, the AutoCompleteBox’s itemsSource binding is lost. AutoCompleteBox is inside that grid. I do assign DataContext directly to the objetc this way:

MyAutoCompleteBox.DataContext = this;

I am wondering if there is a better way to do it? Thank you in advance for the help!

Setting AutoComplete Box:

<sdk:AutoCompleteBox x:Name="MyAutoCompleteBox" IsTextCompletionEnabled="True" ItemsSource="{Binding Items}" />

Code Behind:

public IList<string> Items
    {
        get;
        private set;
    }

    public Basic_ChildWindow()
    {
        InitializeComponent();
        Items = new List<string>();
        Items.Add(@"One");
        Items.Add(@"Two");
        Items.Add(@"Three");

        DataContext = this;
    }

Another datacontext in the same XAML page, AutoCompleteBox is inside that grid:

<Grid x:Name="grdBasic_ChildWindow_Right" St开发者_开发知识库yle="{StaticResource GridStyle}" DataContext="{Binding Source={StaticResource LabelsDataSource}}">


I'm not sure I understand your question--what is "labelsDataSource"?

However, if what you have posted is all the code and there is nothing more to it, simply remove the datacontext/binding from the grid. The grid does not need a datacontext set (it is simply a visual container--not data-related).

So change this:

<Grid x:Name="grdBasic_ChildWindow_Right" Style="{StaticResource GridStyle}" DataContext="{Binding Source={StaticResource LabelsDataSource}}"> 

To this:

<Grid x:Name="grdBasic_ChildWindow_Right" Style="{StaticResource GridStyle}"> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜