Bind DataContext to Current Item in data source using c# code

时间:2017-04-24 17:17:58

标签: c# wpf binding

I know how to bind to the current Item in a Data Source using XAML. Just make the source a resource, set the DataContext to the resource, add a binding Items/.

My question is how do you bind to the current item using c#?

I have an Edit option where I set the context to a new item:

border.DataContext = new Item();

When user clicks save I update the data source:

(Master.ItemsSource as Items)[Master.SelectedIndex] = (Item)border.DataContext;

Now I'm stuck how do I get the DataContext of the border back to the current item using c# code?

border.DataContext = ???

Edit

My XAML code is:

<Border DataContext="{Binding Items/}" Name="border">
    ...
</Border>

My question is if I change the DataContext in code how do I get back the above Binding?

1 个答案:

答案 0 :(得分:0)

在获得大量的投票后,至少我明白了:

它&#39; S:

border.SetBinding(DataContextProperty, "Items/");