简体   繁体   中英

Eventhandler for WPF control in DataTemplate

I've been working with WPF and I have experienced a problem related with DataTemplates. I have a view called DetailPage.xaml and this view uses a DataTemplate called Detail.xaml . I added a textbox to this DataTemplate and I want to handle the TextChanged event. 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:

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:

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. This is your " TextChanged " event.

Note, that by default, TextBox updates Text property, when loosing focus. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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