本文介绍了Silverlight中的DataContext和Command一起使用时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Silverlight 4.0实现MVVM体系结构,我在该用户控件中有一个用户控件TestMVVM.View.EmployeeView
和一个视图EmployeeList. EmployeeList包含一些文本框和一个按钮.我的问题是,当我按照代码中的说明在EmployeeList上使用DataContext时,按钮上的命令停止工作,但是如果我从EmployeeList中删除DataContext,则按钮的命令可以正常工作.
以下是代码
< 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 > < 网格 x:名称 =" LayoutRoot" 已删除 =" 白色" > < grid.rowdefinitions > < 行定义 > < /rowdefinition > < 行定义 > < /rowdefinition > < /grid.rowdefinitions > < grid.column定义 > < 列定义 > < /columndefinition > < 列定义 > < /columndefinition > < /grid.columndefinitions > < sdk:datagrid 自动生成列 =" 错误" 高度 =" 160 " 水平对齐 =" 左侧" 名称 =" dataGrid1" verticalalignment =" 顶部" 宽度 =" 105" itemssource =" {Binding EmployeeList,Mode = OneTime}" selecteditem =" { SelectedEmployee,Mode = TwoWay}" xmlns:sdk =" #unknown" > < sdk:datagrid.columns > < sdk:datagridtextcolumn 标题 =" 年龄" 绑定 =" { Binding Age,Mode = TwoWay}" canuserreorder =" True" canuserresize =" True" canusersort =" True" 宽度 =" 自动" / > < sdk:datagridtextcolumn 标题 =" 名称" 绑定 =" {绑定名称,模式= TwoWay}" canuserreorder =" True" canuserresize =" True" canusersort =" True" 宽度 =" 自动" / > < /sdk:datagrid.columns > 跨度> < /sdk:datagrid > < view:employeelist grid.row =" 0" grid.column =" 1" 数据上下文 =" {Binding SelectedEmployee}" xmlns:view =" #unknown" >> < /view:员工列表 > < /grid > < /usercontrol >
//查看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"> < 网格 x:名称 =" LayoutRoot" 已删除 =" 白色" > < grid.rowdefinitions > < 行定义 高度 =" 20" > < /rowdefinition > < 行定义 高度 =" 20" > < /rowdefinition > < 行定义 高度 =" 20" > < /rowdefinition > < /grid.rowdefinitions > < grid.column定义 > < 列定义 > < /columndefinition > < 列定义 > < /columndefinition > < /grid.columndefinitions > < 文本块 x:名称 =" 名称" 文本 =" 名称" > < /textblock > < 文本框 grid.column =" 1" x:name =" txt_name" 文本 =" {Binding Name,Mode = TwoWay}" > < /textbox > < 文本块 grid.row =" 1" x:name =" age" 文本 =" 年龄" > < /textblock > < 文本框 grid.column =" 1" grid.row =" 1" x:name =" txt_age" 文本 =" {Binding Age,Mode = TwoWay}" >> < /textbox > < 按钮 x:名称 =" btn_Add" Grid.Row =" 3" 内容 =" 添加" 命令 =" {Binding Path = testCommand}" > < /Button > < /grid > < /usercontrol >
解决方案
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>
解决方案
这篇关于Silverlight中的DataContext和Command一起使用时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!