English 中文(简体)
WPF/Silverlight: data binding a datacontext to a property programmatically
原标题:

I ve got some code which sets up a datacontext. Often enough, the datacontext should be set to some underlying data collection, such as an ObservableCollection - but occasionally I d like to set it to a collection that is itself a dependency property.

This can be done in xaml, but doing so defeats the purpose, which is to share the UI code between both scenarios.

Let s say I have a dependency property:

public static readonly DependencyProperty MyDataProperty = [whatever];

and elsewhere, I have a control that expects me to setup the datacontext:

myGreatControl.DataContext = ???

How can I set the above datacontext to refer to the collection stored in the dependency property?

The following question seems related: Silverlight: Programmatically binding control properties

But I d like not to bind one property to another, but a property to a datacontext. The advantage of that is that I don t need to know the type or name or even purpose at the binding code - any FrameworkElement has a datacontext, and I have an (updateable) property I d like to bind to it.

最佳回答

The lovely thing about spending time figuring out how to ask your question is that sometimes you realize it s blindingly obvious...

I said I d like not to bind one property to another, but a property to a datacontext - well, turns out, FrameworkElement.DataContext is just a dependency property; specifically, FrameworkElement.DataContextProperty.

In short, I can just do:

Binding binding = new Binding("MyData") {
    Mode = BindingMode.OneWay,
    Source = this,
};
myGreatControl.SetBinding(FrameworkElement.DataContextProperty, binding);

Sorry bout the question - hopefully this question will save an equally flummoxed coder some time.

问题回答

normally, when you set a binding if you don t explicitly set the object that it is bound to, only the path to the member, it uses the DataContext object.

for example....

<TextBlock Text="{Binding MyProperty}"/>

binds the text to the property "MyProperty" of the DataContext--not a specific collection. You can see this in ControlTemplates, like for ListBox items. As long as your DataContext has the "MyProperty" property you will be fine.

You can also bind directly to the DataContext like this...

<TextBlock Text="{Binding}"/>

I tend to do this with the parent container...

<Grid DataContext="{Binding}">
    <TextBlock Text="{Binding MyProperty}"/>
</Grid>




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签