Silverlight中的DataContext和Command一起使用时不起作用 [英] DataContext and Command in silverlight when used together is not working

查看:59
本文介绍了Silverlight中的DataContext和Command一起使用时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Silverlight 4.0实现MVVM体系结构,我在该用户控件中有一个用户控件TestMVVM.View.EmployeeView和一个视图EmployeeList. EmployeeList包含一些文本框和一个按钮.我的问题是,当我按照代码中的说明在EmployeeList上使用DataContext时,按钮上的命令停止工作,但是如果我从EmployeeList中删除DataContext,则按钮的命令可以正常工作.
以下是代码

 <   usercontrol     x:class   ="   xmlns:x   #unknown" <   usercontrol.datacontext  > 
        <   vm:employeelistviewmodel     xmlns:vm   ="    / > 
    <  /usercontrol.datacontext  > 
    <  网格    ="   LayoutRoot" 已删除  白色" <   grid.rowdefinitions  > 
            <  行定义 >  <  /rowdefinition  > 
            <  行定义 >  <  /rowdefinition  > 
        <  /grid.rowdefinitions  > 
        <   grid.column定义 > 
            <  列定义 >  <  /columndefinition  > 
            <  列定义 >  <  /columndefinition  > 
        <  /grid.columndefinitions  > 
        <   sdk:datagrid    自动生成列  ="  高度   160 " 水平对齐  左侧" 名称   dataGrid1"  verticalalignment   ="    宽度   105"  itemssource   ="   selecteditem    { SelectedEmployee,Mode = TwoWay}"  xmlns:sdk   #unknown" <   sdk:datagrid.columns  > 
                <   sdk:datagridtextcolumn    标题  ="  绑定   { Binding Age,Mode = TwoWay}"  canuserreorder   ="     canuserresize   ="     canusersort   ="  宽度  ="    > 
                <   sdk:datagridtextcolumn    标题  ="  绑定   {绑定名称,模式= TwoWay}"  canuserreorder   ="     canuserresize   ="     canusersort   ="  宽度  ="    > 
            <  /sdk:datagrid.columns  > 跨度>
        <  /sdk:datagrid  > 
        <   view:employeelist     grid.row   ="     grid.column   ="    数据上下文  ="  {Binding SelectedEmployee}"  xmlns:view   ="  >> 

        <  /view:员工列表 > 


    <  /grid  > 
<  /usercontrol  >  



//查看2

 <   usercontrol     x:class   ="   xmlns:x   #unknown" <  网格    ="   LayoutRoot" 已删除  白色" <   grid.rowdefinitions  > 
            <  行定义    ="   20" <   > 
            <  行定义    ="   20" <   > 
            <  行定义    ="   20" <   > 
        <  /grid.rowdefinitions  > 
        <   grid.column定义 > 
            <  列定义 >  <  /columndefinition  > 
            <  列定义 >  <  /columndefinition  > 
        <  /grid.columndefinitions  > 
        <  文本块    ="  名称" 文本  名称" <  > 
        <  文本框    ="   1"  x:name    txt_name" 文本   {Binding Name,Mode = TwoWay}" <  /textbox  > 
        <  文本块    ="   1"  x:name    age" 文本  年龄" <  /textblock  > 
        <  文本框    ="   1"  grid.row    1"  x:name    txt_age" 文本  ="  >> 
        <  /textbox  > 
        <  按钮    ="   btn_Add"  Grid.Row    3" 内容  添加" 命令  ="    >  <  /Button  > 
    <  /grid  > 
<  /usercontrol  >  

解决方案

我意识到该命令是在employeelistviewmodel中定义的.


当您不设置EmployeeListDataContext时,它将继承其父级.因为父级的DataContextemployeelistviewmodel,并且此视图模型包含命令,所以您可以使用该命令来设置按钮的Command属性.如果将EmployeeListDataContext设置为不包含命令的另一个对象,则不会设置按钮的Command属性,并且当您单击按钮时,将不执行命令. /p>

要解决此问题,可以将按钮从EmployeeList移到EmployeeView.


如果必须将按钮保留在EmployeeList中,则可以创建另一个包含SelectedEmployee(原始值设置为DataContext)和所需命令的视图模型,并将其设置为EmployeeList中的>.在EmployeeList中,使用SelectedEmployee和相应的命令.


I am using Silverlight 4.0 to implement MVVM architecture, I have got a usercontrol TestMVVM.View.EmployeeView and a view EmployeeList within this usercontrol. EmployeeList contains some textbox and a button. My problem is when I use DataContext on the EmployeeList as given in the code the command on button stops working but if I remove DataContext from EmployeeList the command of button works fine.
Below is the code

<usercontrol x:class="TestMVVM.View.EmployeeView" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:VM="clr-namespace:TestMVVM.ViewModel"
    xmlns:view="clr-namespace:TestMVVM.View"          
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    >
    <usercontrol.datacontext>
        <vm:employeelistviewmodel xmlns:vm="#unknown" />
    </usercontrol.datacontext>
    <grid x:name="LayoutRoot" removed="White">
        <grid.rowdefinitions>
            <rowdefinition></rowdefinition>
            <rowdefinition></rowdefinition>
        </grid.rowdefinitions>
        <grid.columndefinitions>
            <columndefinition></columndefinition>
            <columndefinition></columndefinition>
        </grid.columndefinitions>
        <sdk:datagrid autogeneratecolumns="False" height="160" horizontalalignment="Left" name="dataGrid1" verticalalignment="Top" width="105" itemssource="{Binding EmployeeList,Mode=OneTime}" selecteditem="{Binding SelectedEmployee,Mode=TwoWay}" xmlns:sdk="#unknown">
            <sdk:datagrid.columns>
                <sdk:datagridtextcolumn header="Age" binding="{Binding Age,Mode=TwoWay}" canuserreorder="True" canuserresize="True" canusersort="True" width="Auto" />
                <sdk:datagridtextcolumn header="Name" binding="{Binding Name,Mode=TwoWay}" canuserreorder="True" canuserresize="True" canusersort="True" width="Auto" />
            </sdk:datagrid.columns>
        </sdk:datagrid>
        <view:employeelist grid.row="0" grid.column="1" datacontext="{Binding SelectedEmployee}" xmlns:view="#unknown">

        </view:employeelist>


    </grid>
</usercontrol>



//view 2

<usercontrol x:class="TestMVVM.View.EmployeeList" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:VM="clr-namespace:TestMVVM.ViewModel" 
    d:DesignHeight="300" d:DesignWidth="400">
    <grid x:name="LayoutRoot" removed="White">
        <grid.rowdefinitions>
            <rowdefinition height="20"></rowdefinition>
            <rowdefinition height="20"></rowdefinition>
            <rowdefinition height="20"></rowdefinition>
        </grid.rowdefinitions>
        <grid.columndefinitions>
            <columndefinition></columndefinition>
            <columndefinition></columndefinition>
        </grid.columndefinitions>
        <textblock x:name="name" text="Name"></textblock>
        <textbox grid.column="1" x:name="txt_name" text="{Binding Name,Mode=TwoWay}">
        </textbox>
        <textblock grid.row="1" x:name="age" text="Age"></textblock>
        <textbox grid.column="1" grid.row="1" x:name="txt_age" text="{Binding Age,Mode=TwoWay}">
        </textbox>
        <Button x:Name="btn_Add" Grid.Row="3" Content="Add" Command="{Binding Path=testCommand}" ></Button>
    </grid>
</usercontrol>

解决方案

As I realized the command is defined in employeelistviewmodel.


When you don''t set the DataContext of EmployeeList, it inherits it from his parent. Because the DataContext of the parent is employeelistviewmodel and this view-model contains the command, the Command property of your button is set with the command. If you set the DataContext of EmployeeList to another object that doesn''t contain the command, the Command property of your button isn''t set and, when you click the button, no command is executed.


For solving the problem, you can move the button from EmployeeList to EmployeeView.


If you have to keep the button in EmployeeList, you can create another view-model that contains SelectedEmployee (the original value you set as DataContext) and the wanted command and, set it as the DataContext of EmployeeList. In EmployeeList, use SelectedEmployee and the command appropriately.


这篇关于Silverlight中的DataContext和Command一起使用时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆