Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 将绑定源更改回XAML中容器内的视图模型_Silverlight_Data Binding - Fatal编程技术网

Silverlight 将绑定源更改回XAML中容器内的视图模型

Silverlight 将绑定源更改回XAML中容器内的视图模型,silverlight,data-binding,Silverlight,Data Binding,我正在使用Silverlight 4和MVVM模式 我的视图模型有两个属性: SomeProperty和 MyCommand SomeProperty是一种复杂类型,具有许多子属性MyCommand是用于处理按钮命令的属性 我有一个子窗口(视图),其网格为LayoutRoot,它绑定到视图模型的SomeProperty属性 <Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">

我正在使用Silverlight 4和MVVM模式

我的视图模型有两个属性:

  • SomeProperty
  • MyCommand
SomeProperty
是一种复杂类型,具有许多子属性
MyCommand
是用于处理按钮命令的属性

我有一个子窗口(视图),其网格为
LayoutRoot
,它绑定到视图模型的
SomeProperty
属性

<Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">
    ...
</Grid>
<UserControl x:Name="MyUserControl">
    <Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">
        <Button Command="{Binding MyCommand}" DataContext="{Binding DataContext, ElementName=MyUserControl}" />
    </Grid>
</UserControl>
但这不起作用,因为
MyCommand
是视图模型的属性,而不是视图模型的
SomeProperty
属性。(当我单击按钮时,它不会执行命令。)

总之,有没有一种方法可以在Silverlight 4中使用数据绑定,这样我就可以让一个容器UI元素显式地设置它的
DataContext
属性,但是在容器中有一个不同的控件引用包含控件的
DataContext
的同级(或父级)属性

我目前的解决方法是在视图的类中定义绑定,但我更希望在XAML中使用它


谢谢

您是否尝试将datacontext添加到绑定?datacontext必须指向viewmodel,因为默认数据上下文是父控件或父数据上下文,在本例中是布局根

我希望这有帮助。
尊敬。

您是否尝试将datacontext添加到绑定?datacontext必须指向viewmodel,因为默认数据上下文是父控件或父数据上下文,在本例中是布局根

我希望这有帮助。
注意。

如果为根元素(ChildWindow、UserControl等)命名,则可以使用ElementName访问视图模型

<Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">
    ...
</Grid>
<UserControl x:Name="MyUserControl">
    <Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">
        <Button Command="{Binding MyCommand}" DataContext="{Binding DataContext, ElementName=MyUserControl}" />
    </Grid>
</UserControl>

或者,这里有另一种方法来做同样的事情

<UserControl x:Name="MyUserControl">
    <Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">
        <Button Command="{Binding DataContext.MyCommand, ElementName=MyUserControl}" />
    </Grid>
</UserControl>

如果为根元素(ChildWindow、UserControl等)命名,则可以使用ElementName访问视图模型

<Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">
    ...
</Grid>
<UserControl x:Name="MyUserControl">
    <Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">
        <Button Command="{Binding MyCommand}" DataContext="{Binding DataContext, ElementName=MyUserControl}" />
    </Grid>
</UserControl>

或者,这里有另一种方法来做同样的事情

<UserControl x:Name="MyUserControl">
    <Grid x:Name="LayoutRoot" DataContext="{Binding SomeProperty, Mode=TwoWay}">
        <Button Command="{Binding DataContext.MyCommand, ElementName=MyUserControl}" />
    </Grid>
</UserControl>

我使用了本文中描述的BindableProxy版本:

在网格上方(可能在UserControl.Resources中),您将创建:

<UserControl.Resources>
    <ns:BindableProxy x:Key="BindableProxy" />
<UserControl.Resources>

然后,在按钮绑定中:

<Button Command="{Binding DataSource.MyCommand, Source={StaticResource BindableProxy}}" />

我使用了本文中描述的BindableProxy版本:

在网格上方(可能在UserControl.Resources中),您将创建:

<UserControl.Resources>
    <ns:BindableProxy x:Key="BindableProxy" />
<UserControl.Resources>

然后,在按钮绑定中:

<Button Command="{Binding DataSource.MyCommand, Source={StaticResource BindableProxy}}" />


是的,在视图的构造函数中,我将视图模型分配给视图的
DataContext
属性。问题在于LayoutRoot将其DataContext指定为视图模型DataContext的子属性,并且在LayoutRoot内的按钮中,我希望引用DataContext中的属性。我希望这是有意义的。是的,在视图的构造函数中,我将视图模型分配给视图的
DataContext
属性。问题在于LayoutRoot将其DataContext指定为视图模型DataContext的子属性,并且在LayoutRoot内的按钮中,我希望引用DataContext中的属性。我希望这是有意义的。这正是我要做的,简单易懂,只是一点XAML。这正是我要做的,简单易懂,只是一点XAML。