Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

XML
<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
XML
<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>
Posted
Updated 1-Dec-11 14:16pm
v3
Comments
[no name] 1-Dec-11 8:31am    
EDIT: added "pre" tag

1 solution

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.

 
Share this answer
 
Comments
Pankaj_Kumar_Jha 7-Dec-11 8:26am    
I got it and solved my problem Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900