Silverlight中的DataContext和Command在一起使用时不起作用

时间:2011-12-01 11:14:52

标签: silverlight-4.0

我正在使用Silverlight 4.0来实现MVVM架构, 我在这个usercontrol中有一个usercontrol TestMVVM.View.EmployeeView和一个EmployeeList视图。 EmployeeList包含一些文本框和一个按钮。 我的问题是当我在代码中给出的EmployeeList上使用DataContext时,按钮上的命令停止工作,但是如果我从EmployeeList中删除DataContext,则按钮的命令工作正常。

<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>

2 个答案:

答案 0 :(得分:0)

如果您在控件上专门设置数据上下文,该控件将创建该类的新实例。因此,您的EmployeeList和EmployeeView将使用不同的数据上下文。

如果未在EmployeeList用户控件上指定数据上下文,则该控件将继承其父级的DataContext(即EmployeeGrid)。通常,您希望控件继承其父级的数据上下文,因此这可能是您想要的配置。

从你的问题中不清楚你想要什么......希望这会有所帮助。

答案 1 :(得分:0)

我遇到了我的问题的解决方案。我要做的是为EmployeeList创建两个viewmodel,为EmployeeView创建第二个,然后EmployeeView的命令将开始执行。