【问题标题】:Fire control event when other control event is fired触发其他控制事件时触发控制事件
【发布时间】:2019-06-20 21:19:31
【问题描述】:

这是我第一次在这里写作。虽然我的第一个问题会更复杂,但我已经厌倦了寻找答案。

我刚开始使用 WPF (MVVM),这里是:

我在一个页面中有 3 个用户 Controls,他们三个是 Control 的同一类。这里重要的是,在第一个TextBox中,当它失去焦点时,它会调用一个方法来计算最后一个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>

我在页面中有 3 次相同的控件,现在我需要在 UserControl2 触发 LostFocus 时也触发 usercontrol3 的 LostFocus。

<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"/>

我到处搜索类似的东西,但我发现了 eventtrigger(因为我没有更改属性而无法工作)、交互(尝试没有成功)。

如果我设置 TextChangedproperty 而不是 Lostfocus,它会起作用,因为它绑定了所有内容,但我不想计算用户输入的每个字符的第二个文本框。

抱歉我的英语错误,感谢您的帮助

已编辑。为了提供帮助,我复制了用户控制和视图的 MVVM 部分后面的代码。

后面的用户控制代码:

    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

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; } }

【问题讨论】:

    标签: c# wpf xaml mvvm user-controls


    【解决方案1】:

    你可以在 UserControl'TextBox 的文本绑定中使用UpdateSourceTrigger="LostFocus" 吗?所以它不会在每个键输入时触发。另外请尝试使用 MVVM 来实现这一切(在 Code Behind 中不挂钩事件),那么您将对操作有很大的控制权。

    我的建议: 在 ViewModel 中,当更改 Codempresa1 时,实现一种修改其他值的方法。您可以包含一个计算它们的方法并在 Codempressa1 的 set 方法中调用该方法,也可以将该方法包含在 PropertyChanged 处理程序中。

    【讨论】:

    • UpdateSourceTrigger="LostFocus" 只通知给自己的用户控制。所有的应用程序都是用 MVVM 构建的。我真正的意思是 usercontrol2 曾经被修改过 文本属性说:-嘿 USERCONTROL3 你必须触发你的 LOSTFOCUS 事件。
    猜你喜欢
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多