tags:

views:

24

answers:

4

Hi,

I have a custom combo box control derived from existing WPF combo box. This custom combo has a DP called AssociatedEnum, once set it evaluates the enum menbers and adds them to Items collection.. However all this enumerating over available values, is done in Combo's loaded event Is it reasonable to assume that all the bindings on Dependency props will be set and evaluated to their initial values during the controls load event.

I am not very sure about this as i have seen some instances where the Binding is evaluated post load event, which defeats entire purpose of writing the selection in load event. My question should i go ahead and implement property changed handler for this dependency property??

Regards, v703218

A: 

My question should i go ahead and implement property changed handler for this dependency property??

You should do this in any case. This is a safer means of evaluating the selection, as without this, you will not be able to support the called changing collections from code.

By handling it here, you are guaranteed to catch the binding, no matter where or how it is setup.

Reed Copsey
A: 

The Loaded event is inherited from FrameworkElement and therefore is a direct tie to the UI. The way you are going about this seems slightly strange but that may be because I am mis-understanding the details.

If you are simply wanting the ComboBox prefilled with data for the user, this can be done by simply binding to a standard ObservableCollection<T> and then setting up a ComboBox Style/Template to suit your presentation needs, more specifically the ComboBox.ItemTemplate as seen below...

        <ComboBox.ItemTemplate> 
            <DataTemplate> 
                <Border>  
                    <StackPanel Orientation="Horizontal"> 
                        <TextBlock Text="{Binding Foo}" /> 
                        <TextBlock Text="{Binding Bar}" /> 
                    </StackPanel> 
                </Border> 
            </DataTemplate> 
        </ComboBox.ItemTemplate> 

the above would be to make use of multiple items on the binding object, however you could also make use of DisplayMemberPath I think you may be doing more then you need to in creating the custom ComboBox.

Aaron
A: 

Thanks Reed... That is what we have been arguing about, in most of the cases me and my colleagues have seen all binding being resolved and set to initial values (not the default or fallback ones before Load it self... However I came across this situation where the binding is being evaluated after my control has loaded, thus missing everything that should be done in LOAD event..

Rest all standard controls (MS WPF controls) bind properly, but my custom controls miss it because everything is done only during load...

Do you have any article which can enlighten us?

v703218
A: 

Hi Reed,

I agree with your reasoning, however I would like to know if Microsoft/WPF creators guarantee that the binding will be evaluated to its first value before the control is loaded...

For example i have a a combo box

and I have code as

public CustomCombo()

{

this.Loaded += OnLoaded; }

private void OnLoaded(Object sender,RoutedEventArgs e)

{

if(CustomDP != null){

//Populate Items inside combo

} }

In such situation if my binding is not being evaluated before my combo is loaded , it would remain empty.. isnt it?

Some us believe that this condition would never arise, and my question to you is whether it is really so, or bindings can be evaluated post load as well, as it might depend on Datacontext or position in Visual/logical tree

v703218