简体   繁体   English

DataTemplate中WPF控件的事件处理程序

[英]Eventhandler for WPF control in DataTemplate

I've been working with WPF and I have experienced a problem related with DataTemplates. 我一直在使用WPF,但遇到了与DataTemplates相关的问题。 I have a view called DetailPage.xaml and this view uses a DataTemplate called Detail.xaml . 我有一个名为DetailPage.xaml的视图,该视图使用了一个名为Detail.xaml的数据模板 I added a textbox to this DataTemplate and I want to handle the TextChanged event. 我向此DataTemplate添加了一个文本框,并且我想处理TextChanged事件。 So I made something like this: 所以我做了这样的事情:

<DataTemplate x:Name="DetailContent">
    <Grid Margin="5" DataContext="{Binding Items[0]}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition MaxHeight="80"/>
        </Grid.RowDefinitions>
        <StackPanel Width="432">
            <TextBox Name="NumeroParadaTB" Text="{Binding NumeroParada}" MaxLength="5" TextChanged="NumeroParadaTB_TextChanged" />
        </StackPanel>
    </Grid>
</DataTemplate>

Then I created and event handler in DetailPage.xaml.cs , like the following: 然后,我在DetailPage.xaml.cs中创建了事件处理程序,如下所示:

protected async void NumeroParadaTB_TextChanged(object sender, TextChangedEventArgs e)
    {
        string nroParada = ((TextBox)sender).Text;

        if(!string.IsNullOrEmpty(nroParada) && nroParada.IsDigitsOnly() && nroParada.Length == 5)
        {

        }
    }

But when running, and error is thrown saying that the event handler doesn't exist. 但是在运行时,抛出错误并指出事件处理程序不存在。 I guess I'm using the eventhandler in a wrong way. 我想我以错误的方式使用了事件处理程序。

Thanks! 谢谢!

Since you're using data binding, I assume, that you have some class with NumeroParada property: 由于您正在使用数据绑定,因此我假设您有一些具有NumeroParada属性的类:

public class SomeClass : INotifyPropertyChanged
{
    /* other code here */

    public string NumeroParada
    {
         get { return numeroParada; }
         set
         {
             if (numeroParada != value)
             {
                  numeroParada = value;
                  OnPropertyChanged("NumeroParada");
             }
         }
    }
    private string numeroParada;    
}

Setter of this property will fire, when UI will update the binding source. 当UI将更新绑定源时,将触发此属性的设置器。 This is your " TextChanged " event. 这是您的“ TextChanged ”事件。

Note, that by default, TextBox updates Text property, when loosing focus. 请注意,默认情况下,失去焦点时, TextBox更新Text属性。 If you want to perform any action when user changes text, update your binding definition: 如果要在用户更改文本时执行任何操作,请更新绑定定义:

Text="{Binding NumeroParada, UpdateSourceTrigger=PropertyChanged}"

So far so good. 到现在为止还挺好。 But this code: 但是这段代码:

if(!string.IsNullOrEmpty(nroParada) && nroParada.IsDigitsOnly() && nroParada.Length == 5)

suggests, that you're trying to implement validation of value, entered by user. 建议您正在尝试实施由用户输入的价值验证。 Validation in WPF is rather big theme, I'd recommend you to read something like this to select validation approach. 验证在WPF是相当大的主题,我建议你阅读像这样选择验证方法。

Instead of adding an Event handler, you can use Event to Command logic. 您可以使用“事件到命令”逻辑来代替添加事件处理程序。 Create a Command in ViewModel and bind it to the TextChanged event. 在ViewModel中创建一个Command并将其绑定到TextChanged事件。

        <TextBox Text="{Binding SearchText, Mode=TwoWay}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <i:InvokeCommandAction Command="{Binding MyCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>

Interaction triggers available in System.Windows.Interactivity assembly. System.Windows.Interactivity程序集中提供了交互触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM