DataGrid的DataTemplate中TextBox上的触发器不起作用

时间:2012-09-27 07:12:07

标签: c#-4.0 mvvm .net-4.0 silverlight-4.0

MVVM in Silverlight 4.0工作时。我遇到了以下问题 请参阅以下Xaml。请注意,在TextBox1中,方法UpdateText(在交互式触发器下)正在运行,但在TextBox2这是DataGrid's模板的一部分,此方法“UpdateText”无效一点都不任何人都可以帮助我在TextChanged TextBox2事件上调用方法吗? (UpdateText只是MVVM类使用TextBox1而不是TextBox2的简单方法)

这是xaml:

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="MVVMEventTriggerDemo.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clr-namespace:MVVMEventTriggerDemo.ViewModels"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:si="clr-namespace:Expression.Samples.Interactivity;assembly=Expression.Samples.Interactivity"
Height="600" Width="700">
<UserControl.Resources>
    <viewModel:MainViewModel x:Key="MainViewmodel"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource MainViewmodel}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="258*" />
        <ColumnDefinition Width="262*" />
    </Grid.ColumnDefinitions>
    <TextBox x:Name="textbox1" Text="{Binding EmployeeName, Mode=TwoWay}" Width="100" Height="30" Margin="142,77,20,289" Grid.Column="1">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <si:CallDataMethod Method="UpdateText"/>
                <si:ShowMessageBox Caption="Thank you"
                                   Message="Thanks for trying the Example"
                                   MessageBoxButton="OK"/>
                <si:SetProperty TargetName="textbox1" PropertyName="Background" Value="PaleGoldenrod"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
    <Button Content="Show Message" Width="100" Height="25" Margin="142,113,20,258" Grid.Column="1">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <si:CallDataMethod Method="HandleShowMessage"/>
                <si:ShowMessageBox Caption="Thank you"
                                   Message="Thanks for trying the Example"
                                   MessageBoxButton="OK"/>
                <si:SetProperty TargetName="LayoutRoot" PropertyName="Background" Value="PaleGoldenrod"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
    <sdk:DataGrid 
        x:Name="dgDevice" 
        AutoGenerateColumns="False" 
        Margin="13,13,13,13"
        BorderThickness="1,0,1,1"
        RowBackground="White"
        VerticalScrollBarVisibility="Auto" Width="320" 
        Opacity="0.9"
        Background="White"
        GridLinesVisibility="None"
        HeadersVisibility="None"
        HorizontalScrollBarVisibility="Disabled"
        ItemsSource="{Binding ActualColors,Mode=TwoWay}">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn Width="320" >
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid Width="auto">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="30*"  />                                   
                                <ColumnDefinition Width="20*" />
                            </Grid.ColumnDefinitions>

           <TextBox x:Name="textbox2" Text="{Binding EmployeeName, Mode=TwoWay}" Width="100" Height="30" Margin="142,77,20,289" Grid.Column="1">
            <i:Interaction.Triggers>
              <i:EventTrigger EventName="TextChanged">
                <si:CallDataMethod Method="UpdateText"/>
                <si:ShowMessageBox Caption="Thank you"
                                   Message="Thanks for trying the Example"
                                   MessageBoxButton="OK"/>
                <si:SetProperty TargetName="textbox2" PropertyName="Background" Value="PaleGoldenrod"/>
                 </i:EventTrigger>
               </i:Interaction.Triggers>
            </TextBox>
       </Grid>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>
</Grid>

1 个答案:

答案 0 :(得分:0)

我不确定它可以帮助你但是在Datatemplate绑定中会像

一样
<TextBlock Text="{Binding Path=DataContext.BusyText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />

我认为你可能必须在上句中绑定与文本绑定相同的事件。