简体   繁体   中英

Fire control event when other control event is fired

This is my first time writing here. I though my first question would by more complex but I am tired of searching for an answer.

I just started with WPF (MVVM), here it goes:

I have 3 user Control s in a Page, the three of them are the same class of Control . The important thing here is that in the first TextBox , when it lost focus, it calls a method to calculate the last TextBox.Text .

<UserControl x:Class="ASSEMBLY.View.UserControls.EtiquetaDinamicaUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:ASSEMBLY.View.UserControls"
         x:Name="userControl"
         mc:Ignorable="d" 
         Background="Orange" BorderThickness="0"  >

<DockPanel VerticalAlignment="Stretch">
    <Grid Background="green">
        <Grid.ColumnDefinitions >
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Viewbox  Grid.Column="0" VerticalAlignment="Stretch" Stretch="Fill" >
            <Label Grid.Column="0" Content="{Binding NombreEtiqueta, ElementName=userControl, Mode=TwoWay}"  HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,0,5,0"
             Background="White" VerticalAlignment="Stretch"  />
        </Viewbox>
        <Viewbox  Grid.Column="1" VerticalAlignment="Stretch" Stretch="Fill" >
            <TextBox Grid.Column="1" MinWidth="30"  BorderThickness="0" Margin="0,0,5,0"
             VerticalContentAlignment="Center" HorizontalContentAlignment="Left" LostFocus="IdChanged" Loaded="IdChanged"
             Text="{Binding IdValue, ElementName=userControl, Mode=TwoWay}"
             />
        </Viewbox>
        <Viewbox  Grid.Column="2" VerticalAlignment="Stretch" Stretch="Fill" >
            <TextBox Grid.Row="0" HorizontalAlignment="Stretch" IsEnabled="True" MinWidth="100"  BorderThickness="0"
                TextAlignment="Left" VerticalAlignment="Stretch" 
               Text="{Binding Path= Descripcion, ElementName=userControl, Mode=TwoWay}">
            </TextBox>
        </Viewbox>

    </Grid>
</DockPanel>

I have 3 times the same control in the Page, now I need that when the UserControl2 fires LostFocus also fire the LostFocus of the usercontrol3.

<controls:EtiquetaDinamicaUserControl Grid.Row="0" Grid.Column="0" Margin="5,2,0,0" IdValue="{Binding Codempresa1, Mode=TwoWay}" NombreEtiqueta="TEST1"/>
<controls:EtiquetaDinamicaUserControl x:Name="UserControl2" Grid.Row="1" Grid.Column="0" Margin="5,2,0,0" IdValue="{Binding Codempresa2, Mode=TwoWay}" NombreEtiqueta="TEST2"/>
<controls:EtiquetaDinamicaUserControl x:Name="UserControl3" Grid.Row="2" Grid.Column="0" Margin="5,2,0,2" IdValue="{Binding Codempresa3, Mode=TwoWay}" NombreEtiqueta="TEST3"/>

I searched everywhere for something similar, but I found eventrigger (not working because I am not changing a property), interaction (no success trying it).

It works if I set the TextChanged property instead of Lostfocus because everything it's bound, but I don't want to be calculating the second textbox every character the user input.

Sorry for my english mistakes and thank you for your help

EDITED. IN ORDER TO HELP I COPY THE CODE BEHIND OF THE USERCONTROL AND THE MVVM PART OF THE VIEW.

USERCONTROL CODE BEHIND:

    public static readonly DependencyProperty nombreEtiqueta =
    DependencyProperty.Register("NombreEtiqueta", typeof(string), typeof(EtiquetaDinamicaUserControl), new
    PropertyMetadata("DEF"));

    public string NombreEtiqueta
    {
        get { return (string)GetValue(nombreEtiqueta); }
        set { SetValue(nombreEtiqueta, value); }
    }

    public static readonly DependencyProperty SetDescripcionProperty =
        DependencyProperty.Register("Descripcion", typeof(string), typeof(EtiquetaDinamicaUserControl), new
        PropertyMetadata("SIN ASIGNAR"));

    public string Descripcion
    {
        get { return (string)GetValue(SetDescripcionProperty); }
        set { SetValue(SetDescripcionProperty, value); }
    }


    public static DependencyProperty SetIdValueProperty =
        DependencyProperty.Register("IdValue", typeof(string), typeof(EtiquetaDinamicaUserControl), new
        PropertyMetadata("0"));

    public string IdValue
    {
        get { return (string)GetValue(SetIdValueProperty); }
        set { SetValue(SetIdValueProperty, value);
        }
    }




    #region Evento al perder foco IdValue

    private void IdChanged(object sender, RoutedEventArgs e)
    {
        try
        {
            int tmp_id = Convert.ToInt32(((TextBox)e.Source).Text);
            if (tmp_id != 0)
            {
                Descripcion = tmp_id.ToString();
            }

        }
        catch
        {
            Descripcion = "SOLO NUMEROS";
        }

    }
    #endregion

MVVM from the View

class PaginaReservaViewModel:ViewModelBase
{
    Reserva reserva;
    #region Atributos Agencias
    private int codempresa1;
    private int codempresa2;
    private int codempresa3;

    #endregion

    #region Get/Set Agencias
    public int Codempresa1 { get { return codempresa1; } set { codempresa1 = value; } }
    public int Codempresa2
    {
        get { return codempresa2; }
        set
        {
            codempresa2 = value;
            if (codempresa3 == 0)
            {
                codempresa3 = codempresa2;
                OnPropertyChanged("Codempresa3");

            }
        }
    }
    public int Codempresa3 {
        get { return codempresa3; }
        set {
            codempresa3 = value; } }

Can you use UpdateSourceTrigger="LostFocus" in UserControl' TextBox's Text binding? so it wont fire on every key input. Also please try to implement this all with MVVM (Without hooking events in Code Behind), then you will have much control over the operation.

My Suggestion: In ViewModel implement a way to modify the other values, when changing Codempresa1. You can include a method to calculate them and call the method in set method of Codempressa1 or you can include the method in PropertyChanged handler.

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