【问题标题】:Triggers on TextBox in DataGrid's DataTemplate are not WorkingDataGrid 的 DataTemplate 中的 TextBox 上的触发器不起作用
【发布时间】:2012-09-27 07:12:06
【问题描述】:

在处理MVVM in Silverlight 4.0 时。我遇到了以下问题。
请参阅以下Xaml。请注意,在 TextBox1 中,方法 UpdateText(在交互触发器下)正在工作,但在 TextBox2 中,这是 DataGrid's 模板的一部分,这里的方法“UpdateText”根本不起作用。谁能帮我在TextBox2TextChanged事件上调用一个方法? (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>

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 我尝试了您的建议,但在所提问题的上下文中它并不能帮助/解决问题。如果可能,请使用更多相关信息更新答案。
    • @SaurabhRai:你有父控件 UserControl?你能用你的代码发布问题,以便我可以很好地提出建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2012-03-10
    • 2016-05-08
    相关资源
    最近更新 更多