Ticket T986239
Visible to All Users

An easy way to refer to DataContext in UserControl View

created 4 years ago

Hello

When using Binding,
I'd like to know an easy way to refer to DataContext in View. We are using the MVVM module of DevExpress.

When we currently want to bind to a variable in UserControl View, rather than a dependent property of any object, we define the name of the View to set up ElementName and bind it.

ex)

XAML
<UserControl x:Name="View"> Value={Binding DataContext.ViewVar, ElementName=View}

In this format, you access DataContext in View of UserControl. However, I would like to know how to refer to DataContext in UserControl View through the dxci extension or dxmvvm extension instead of setting up this ElementName.

Because if you set the ElementName property and refer to DataContext in View, you have to set ElementName on each screen and write it down every time you bind it, so there is a big opportunity cost for all screens.

Please tell me how to access DataContext in UserControl View. When defining UserControl, we do not define DataContxt as ViewModel, but connect View and ViewModel by MVVM Module Injection method of DevExpress.

Please reply as soon as possible.

Answers approved by DevExpress Support

created 4 years ago

Hello Hanmi,

DataContext is an inheritable property, so if child elements don't have their own DataContext setters, you don't need to use ElementName/AncestorType:

XAML
<UserControl DataContext="test value"> <Grid> <TextBlock Text="{Binding}" /> </Grid> </UserControl>

As for a ready-to-use attached property for accessing parent objects' DataContext, we don't have such properties in our suite. WPF offers a wide variety of binding types that are sufficient to access the parent elements without extra properties. In any case, you're free to implement your own attached property if that's suitable for your implementation.

    Show previous comments (8)

      Hi,
      Andrey.

      GridControl has successfully referenced DataContext in UserControld via View.DataContext.

      When I used the corresponding View.DataContext in LookupEdit, the code does not work.
      Maybe it's because LookupEdit doesn't have View properties. In this case, how can I refer to DataContext in UserControl?

      Andrey Marten (DevExpress Support) 4 years ago

        Hello,

        I'm afraid I cannot give you precise recommendations based only on this information. Is this issue still specific only to applications based on MIF? How is this LookUpEdit declared? Where do you use this editor? Where do you use the binding definition with the ElementName property? What do you see in the data context of the target element using tools similar to Snoop? If this issue is specific to a certain editor, I recommend that you create a separate thread and describe this issue in greater detail. Additionally, share all related XAML and code files or create a simple sample where we can see the same issue. All this will help us process your inquiry in the most efficient manner.

        Thanks,
        Andrey

          I solved the problem.

          Thank you.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.