Silverlight 4.0 嵌套控件中的Silverlight数据绑定

Silverlight 4.0 嵌套控件中的Silverlight数据绑定,silverlight-4.0,mvvm,mvvm-light,Silverlight 4.0,Mvvm,Mvvm Light,大家好,我正在使用SL4和MVVM应用程序,事实上,我被困在一些地方,我可能做了一些错误的事情,这就是为什么需要你们的帮助,这是我的场景 //suodo code public class EmployeeModel { //code Public List<Shifts> Employeeshifts{get;set;} } public class ShiftModel { //code } ClickBindingCommand在主VM中定义,但它

大家好,我正在使用SL4和MVVM应用程序,事实上,我被困在一些地方,我可能做了一些错误的事情,这就是为什么需要你们的帮助,这是我的场景

//suodo code 
public class EmployeeModel
{
    //code
    Public List<Shifts> Employeeshifts{get;set;}
}

public class ShiftModel
{
    //code
}
ClickBindingCommand
在主VM中定义,但它绑定在shift控件中,并且shift控件的数据上下文是shift类,它是我的模型类。如果我在我的shift模型类中声明了这个命令,那么这意味着如果我单击shift控件,就会调用这个属性,但我不想要它,因为我想要它在我的主视图模型中,我错在哪里

我是否应该在我的shift模型类中声明它,但这样我将直接将我的模型绑定到我的视图?

使用或绑定。这将允许您的命令在MainViewModel上绑定/激发

另一种选择是使用框架。然后,您可以将操作附加到子级中的按钮,然后单击事件将弹出到父级。

此“工具包”提供了silverlight中FindAncestor的实现。试试这个:


我遇到了同样的问题,解决这个问题的方法是命名我的UserControl,然后在绑定中引用该名称

以下是语法:

{Binding ElementName=SomeTextBox,Path=Text}

绑定到元素XAML的“Text”属性 具有name=“SomeTextBox”或x:name=“SomeTextBox”的元素。

下面是我的用户控件:

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             
             x:Class="SupportReports.Workflow.Portfolio.PortfolioManager"
             mc:Ignorable="d"
             Name="PortfolioManagerControl"
             >

这里是绑定到我的主视图模型中的命令的嵌套数据模板

<cmd:EventToCommand Command="{Binding ElementName=PortfolioManagerControl, Path=DataContext.PortfolioManagerProjectSelectedCommand}" CommandParameter="{Binding Text, ElementName=ProjectName}" />

请将您的问题标题更新为与您的问题更相关的内容-它目前只是一个标签列表,属于“标签”部分。
<ItemsControl ItemSource={Binding EmployeeShifts}>
    <ItemTemplate>
        <DataTemplate>
            <controls:ShiftControl  Click={Binding ClickBindingCommand}/>//here is problem this command is in mainviewmodel
        </DataTemplate>
    </ItemTemplate>
</ItemsControl>
this.DataContext = new MainVM();
<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             
             x:Class="SupportReports.Workflow.Portfolio.PortfolioManager"
             mc:Ignorable="d"
             Name="PortfolioManagerControl"
             >
<cmd:EventToCommand Command="{Binding ElementName=PortfolioManagerControl, Path=DataContext.PortfolioManagerProjectSelectedCommand}" CommandParameter="{Binding Text, ElementName=ProjectName}" />