【问题标题】:Silverlight Data Binding in Nested Controls嵌套控件中的 Silverlight 数据绑定
【发布时间】:2011-08-24 11:33:53
【问题描述】:

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

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

public class ShiftModel
{
    //code
}

主页面的视图模型

public class MainVM
{
    MainVM()
    {
        EmployeeList = DateFromSomeService;
    }
    Public List<Employees> EmployeeList{get;set}
    Public DelegateCommand ClickBindingCommand{get;set;} 
}

MainPage.xaml

<ItemsControl ItemSource={Binding EmployeeList}>
    <ItemTemplate>
        <DataTemplate>
            <controls:EmployeeControl/>
        </DataTemplate>
    </ItemTemplate>
</ItemsControl>

Employeecontro.xaml

<ItemsControl ItemSource={Binding EmployeeShifts}>
    <ItemTemplate>
        <DataTemplate>
            <controls:ShiftControl  Click={Binding ClickBindingCommand}/>//here is problem this command is in mainviewmodel
        </DataTemplate>
    </ItemTemplate>
</ItemsControl>

MainPage.cs

this.DataContext = new MainVM();

ClickBindingCommand 是在主 VM 中定义的,但它绑定在班次控制中,班次控制的数据上下文是班次类,这是我的模型类。如果我在我的班次模型类中声明这个命令而不是它的工作,意味着如果我点击班次控制这个属性被调用但我不想要这个,因为我想在我的主视图模型中,我错在哪里?

我应该在我的班次模型类中声明它,但这样我会直接将我的模型绑定到我的视图吗?

【问题讨论】:

  • 请将您的问题标题更新为与您的问题更相关的内容 - 它目前只是标签列表,属于“标签”部分。

标签: silverlight-4.0 mvvm mvvm-light


【解决方案1】:

我遇到了同样的问题,为我解决的问题是为我的 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"
             >

这是在我的主视图模型中绑定到我的命令的嵌套 dataTemplate

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

【讨论】:

    【解决方案2】:

    使用DataContextProxyRelativeSource 绑定。这将允许您的 Command 在您的 MainViewModel 上绑定/触发。

    另一种选择是使用Caliburn Micro 框架。然后,您可以将操作附加到子按钮中的按钮,点击事件将冒泡到父按钮。

    【讨论】:

      【解决方案3】:

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

      http://blog.thekieners.com/2010/09/08/relativesource-binding-with-findancestor-mode-in-silverlight/

      【讨论】:

        猜你喜欢
        • 2011-05-04
        • 1970-01-01
        • 2011-05-05
        • 1970-01-01
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 2023-03-13
        • 2013-03-30
        相关资源
        最近更新 更多