DataContext and Command in silverlight when used together is not working

穿精又带淫゛_ 提交于 2019-12-13 03:34:27

问题


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.

<UserControl x:Class="TestMVVM.View.EmployeeView"
    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/>
    </UserControl.DataContext>
    <Grid x:Name="LayoutRoot" Background="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}" >
            <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}" >

        </view:EmployeeList>


    </Grid>
</UserControl>
//view 2
<UserControl x:Class="TestMVVM.View.EmployeeList"
    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" Background="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>

回答1:


If you specifically set the data context on your control, that control will create a new instance of that class. Hence, your EmployeeList and EmployeeView will be using different data context.

When you do not specifiy the data context on the EmployeeList user control, that control is inheriting the DataContext of its parent (ie, EmployeeGrid). Typically, you WANT the control to inherit the data context of its parent so this is probably the configuration you are wanting.

It wasnt clear from your question what you were wanting... hope this helps.




回答2:


I came across the solution of my problem.What I have to do is to create two viewmodel one for EmployeeList and the second for EmployeeView and then the command of EmployeeView will start executing.



来源:https://stackoverflow.com/questions/8340434/datacontext-and-command-in-silverlight-when-used-together-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!