简体   繁体   English

如何在MVMVM WPF中绑定父项或其他ViewModel属性

[英]How to bind parent or other viewmodel property in mvvm wpf

Hi I had defined Property in My BookViewVM 嗨,我在BookViewVM中定义了Property

   private Visibility _isLoaderPageView = Visibility.Hidden;
    public Visibility IsLoaderPageView
    {
        get
        {
            return _isLoaderPageView;
        }
        set
        {
            _isLoaderPageView = value;
            RaisePropertyChanged("IsLoaderPageView");
        }
    }

I am calling this bookViewVM on Page.xaml page but on Page.xaml some other PageViewVM had already bind But I have to call the BookViewVM Property: 我在Page.xaml页面上调用此bookViewVM,但在Page.xaml上已经绑定了其他一些PageViewVM,但是我必须调用BookViewVM属性:

<views:LoadingAnimation  x:Name="ldrControl"  Margin="100,100,100,150"   Visibility="{Binding IsLoaderPageView}"   />

I am able to access this BookViewVM Property on My Pageview.xaml.cs code Like this: ETBApp.MainVM.BookVM.IsLoaderPageView 我可以在My Pageview.xaml.cs代码上访问此BookViewVM属性,如下所示:ETBApp.MainVM.BookVM.IsLoaderPageView

But I have to call this from Property binding on page. 但是我必须从页面的属性绑定中调用它。 please tell me how to bind it. 请告诉我如何绑定它。

Both ViewModel like BookViewVm and PageViewVm on same namesapce. 两个ViewModel都在相同的名称空间上喜欢BookViewVm和PageViewVm。

The problem is that you have multiple DataContexts and by default Binding works with the DataContext of current element if it is specified, and datacontext of parent if not(and so on). 问题是您有多个DataContext ,默认情况下,如果指定了绑定 ,则绑定将与当前元素的DataContext一起使用 ;如果未指定,则与父元素的datacontext一起使用(依此类推)。

so there is 2 simple solutions: 所以有2个简单的解决方案:

  • specify DataContext of element directly(only if that element does not have one) 直接指定元素的DataContext (仅当该元素没有一个时)
  • use ElementName (if you have access in xaml to specific element and it must be named properly through Name or x:Name ), or RelativeSource (if you bind to some ancestor ) inside Binding with Path 使用ElementName (如果您可以在xaml中访问特定元素,并且必须通过Namex:Name正确命名 ),或在Path with Binding中使用RelativeSource (如果您绑定到某些祖先

Just set properly DataContext. 只需正确设置DataContext。 For example in grid 例如在网格中

<Grid.DataContext>
  <viewmodel:YourViewModel />
</Grid.DataContext>

And now you can bind it 现在您可以绑定它

<ItemsControl ItemsSource="{Binding Model.ButtonsRaw1}">

There we bind ItemsSource from our model. 在那里,我们从模型中绑定ItemsSource。

Or you have an alternative way to do it from Resource. 或者,您也可以使用Resource的另一种方法。 SOmething like this 像这样

<UserControl.Resources>
        <Style TargetType="local:YourType">
            <Setter Property="Text" Value="{Binding Path=DataContext.Model.Text, Mode=TwoWay,ElementName=YourGridName}"/>
        </Style>
</UserControl.Resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将列 header 绑定到 ViewModel 中的属性? (WPF MVVM) - How to bind column header to property in ViewModel? (WPF MVVM) 如何在WPF MVVM中将View变量绑定到ViewModel? - How to bind Variables of View to ViewModel in WPF MVVM? WPF / MVVM-从XAML初始化图像并绑定到ViewModel中的属性 - WPF/MVVM - Initialize image from XAML and bind to property in ViewModel WPF/MVVM:如何从 ItemsControl 将模型绑定到视图/视图模型 - WPF/MVVM : How to bind model to view/viewmodel from ItemsControl 如何在WPF中绑定两个散点图(MVVM,ViewModel-First)? - How to bind two scatter charts in WPF (MVVM, ViewModel-First)? 如何将View控件正确绑定到ViewModel列表(WPF MVVM) - how to correctly bind a View control to a ViewModel List (WPF MVVM) MVVM-WPF如何将我的视图绑定到我的Viewmodel? - MVVM - WPF How do i bind my View to my Viewmodel? WPF MVVM:如何将GridViewColumn绑定到ViewModel-Collection? - WPF MVVM: how to bind GridViewColumn to ViewModel-Collection? WPF:是否可以绑定到userControl属性的父级并将其DataContext设置为视图模型? - WPF: Is it possible to bind to a parent on userControl property and set its DataContext to a viewmodel? 如何将UserControl的DependencyProperty绑定到MVVM的ViewModel中的属性? - How to bind a DependencyProperty of a UserControl to a property in it's ViewModel in MVVM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM