tags:

views:

45

answers:

2

I have a window which has a usercontrol in it . This usercontrol's RequestObject property bound to SearchArgumentObject property of ViewModel of the window.

This is listing from my window class

<Grid DataContext="{Binding SearchArgumentObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <guiLib:RegCardSearchForm x:Name="SearchParametrsUC" Grid.Row="1" RequestObject="{Binding .,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>

In Usercontrol class I created dependency property:

This is listing from my userControl class

    public static DependencyProperty RequestObjectProperty = DependencyProperty.Register("RequestObject", typeof(RegistrationCardSearch), typeof(RegCardSearchForm));

    public RegistrationCardSearch RequestObject
    {
        get
        {
            return (RegistrationCardSearch)GetValue(RequestObjectProperty);
        }
        set
        {
            SetValue(RequestObjectProperty, value);
        }
    }

On the level of the usecontrol everything works fine and RequestOject property changed.

But in my window class I can't see modification of SearchArgumentObject property which was made in usercontrol.

How can I get modefied property value? I think answer to this question is very trivial but I can't find solution.

A: 

The code for your Window class is setting the DataContext of the Grid to a property obtained from a binding to a property on another object's DataContext further up the tree. Do you have the Window's DataContext set elsewhere?

Let's say that the object which is supplying the SearchArgumentObject is named SearchWindowViewModel. In the code-behind of the Window, you would have the following code (in the constructor, for example):

DataContext = new SearchWindowViewModel();

Now, all the properties that SearchWindowViewModel exposes are available to the Window. To bind the SearchWindowViewModel.SearchArgumentObject to the UserControl's RequestObject property, you would have the following XAML:

<Grid>
  <guiLib:RegCardSearchForm x:Name=SearchParametersUC Grid.Row=1 
          RequestObject={Binding SearchArgumentObject />
</Grid>

If you don't want to set the Window's DataContext, you can set the Grid's DataContext using the same type of code as I used above, and the binding in the XAML would remain the same.

Hope that helps.

Eric
I am already use this sort of binding. It didn't work thats why I get binding from Grid. But it is very strange. {Binding} works normal on userControl but {Binding SearchArgumentObject} not works. I check DataContext everything is normal. Problem in usercontrol binding
Polaris
+1  A: 

Setting the DataContext on the Grid isn't doing anything but breaking the two-way linking of your properties. Skip the extra step and bind the VM property to the control property that you want to pick up changes from instead:

<Grid>
    <guiLib:RegCardSearchForm x:Name="SearchParametrsUC" Grid.Row="1"
           RequestObject="{Binding SearchArgumentObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
John Bowen
i am already do that. It didn't work. That why i use additional step :(
Polaris
Your additional step made things worse. Have you verified that your ViewModel is being used as the DataContext for the Window? You should also make sure you aren't setting the DataContext for your UC internally in its XAML or code-behind. I would suggest using Snoop to examine your DataContexts at runtime.
John Bowen
DataContext set properly. For example when i replace binding with this <TextBlock Text="{Binding SearchArgumentObject}"/> he call Get accessor of the SearchArgumentObject property, but with usercontrol and my property debugger don't enter to the get accesssor of SearchArgumentObject property
Polaris
Have you looked at the DataContext of your UC? A common error is doing something like DataContext = this; in the UC code which overrides the inherited DataContext and can break external bindings.
John Bowen