【问题标题】:WPF MVVM switch focus between 2 text boxesWPF MVVM在2个文本框之间切换焦点
【发布时间】:2016-08-13 12:11:47
【问题描述】:

我在 MVVM 中有一个带有 2 个文本框的 WPF 应用程序,我想动态设置焦点

在 WPF 中,我为文本框“NodeBarcode_1”和“NodeBarcode_2”创建了 2 个数据触发器

 <DataTrigger Binding="{Binding NodeBarcode_1_GetFocusNow}" Value="True">
                    <Setter Property="FocusManager.FocusedElement" Value ="{Binding ElementName=NodeBarcode_1}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding NodeBarcode_2_GetFocusNow}" Value="True">
                    <Setter Property="FocusManager.FocusedElement" Value ="{Binding ElementName=NodeBarcode_2}"/>
                </DataTrigger>

在视图模型中,我创建了 2 个布尔变量:

public const string NodeBarcode_1_GetFocusNowPropertyName = "NodeBarcode_1_GetFocusNow";
private bool _NodeBarcode_1_GetFocusNow = false;
public bool NodeBarcode_1_GetFocusNow
{
    get
    {
        return _NodeBarcode_1_GetFocusNow;
    }

    set
    {
        if (_NodeBarcode_1_GetFocusNow == value)
        {
            return;
        }

        _NodeBarcode_1_GetFocusNow = value;
        RaisePropertyChanged(NodeBarcode_1_GetFocusNowPropertyName);
    }
}

public const string NodeBarcode_2_GetFocusNowPropertyName = "NodeBarcode_2_GetFocusNow";
private bool _NodeBarcode_2_GetFocusNow = false;
public bool NodeBarcode_2_GetFocusNow
{
    get
    {
        return _NodeBarcode_2_GetFocusNow;
    }

    set
    {
        if (_NodeBarcode_2_GetFocusNow == value)
        {
            return;
        }

        _NodeBarcode_2_GetFocusNow = value;
        RaisePropertyChanged(NodeBarcode_2_GetFocusNowPropertyName);
    }
}

我创建了 2 个在用户在文本框中输入条形码时执行的方法

private void NodeBarcode_1_Execute(EventArgs e)
{
    try
    {
        IProduct NodeObj = ObjectFactory<IProduct>.Create("Node");
        NodeObj.Barcode = NodeBarcode_1_txt;
        NodeObj.Validation();
        labelAppObj.AddProduct(NodeObj);
        NodeBarcode_2_GetFocusNow = true;

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        NodeBarcode_1_txt = string.Empty;
    }


}
private void NodeBarcode_2_Execute(EventArgs e)
{
    try
    {
        IProduct NodeObj = ObjectFactory<IProduct>.Create("Node");
        NodeObj.Barcode = NodeBarcode_2_txt;
        NodeObj.Validation();
        labelAppObj.AddProduct(NodeObj);
        NodeBarcode_1_GetFocusNow = true;


    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        NodeBarcode_2_txt = string.Empty;
    }

}

当用户扫描文本框“NodeBarcode_1”中的第一个条形码时,焦点转到“NodeBarcode_2”。没问题。但是当用户扫描“NodeBarcode_2”中的下一个条形码时,我希望焦点回到“NodeBarcode_1”。但这不会发生。

什么会导致这个问题?

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    您必须重置您的状态,以使触发器像这样连续工作:

    private void NodeBarcode_1_Execute(EventArgs e) {
        try {
            IProduct NodeObj = ObjectFactory<IProduct>.Create("Node");
            NodeObj.Barcode = NodeBarcode_1_txt;
            NodeObj.Validation();
            labelAppObj.AddProduct(NodeObj);
            NodeBarcode_1_GetFocusNow = false; // <--- Here
            NodeBarcode_2_GetFocusNow = true;
    
        }
        catch (Exception ex) {
            MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            NodeBarcode_1_txt = string.Empty;
        }
    }
    
    private void NodeBarcode_2_Execute(EventArgs e) {
        try {
            IProduct NodeObj = ObjectFactory<IProduct>.Create("Node");
            NodeObj.Barcode = NodeBarcode_2_txt;
            NodeObj.Validation();
            labelAppObj.AddProduct(NodeObj);
            NodeBarcode_1_GetFocusNow = true;
            NodeBarcode_2_GetFocusNow = false; // <--- Here
    
        }
        catch (Exception ex) {
            MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            NodeBarcode_2_txt = string.Empty;
        }
    }
    

    编辑

    我做了一个最小的例子。我并没有真正更改代码,因此除非您没有说出所有事实,否则此解决方案必须适用于您的问题。

    Xaml

     <StackPanel Grid.Row="1">
                <TextBox x:Name="NodeBarcode_1" Background="Green">
                    <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding NodeBarcode_1_GetFocusNow}" Value="True">
                                    <Setter Property="FocusManager.FocusedElement" Value ="{Binding ElementName=NodeBarcode_1}"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding NodeBarcode_2_GetFocusNow}" Value="True">
                                    <Setter Property="FocusManager.FocusedElement" Value ="{Binding ElementName=NodeBarcode_2}"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
                <TextBox x:Name="NodeBarcode_2" Background="Blue">
                    <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding NodeBarcode_1_GetFocusNow}" Value="True">
                                    <Setter Property="FocusManager.FocusedElement" Value ="{Binding ElementName=NodeBarcode_1}"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding NodeBarcode_2_GetFocusNow}" Value="True">
                                    <Setter Property="FocusManager.FocusedElement" Value ="{Binding ElementName=NodeBarcode_2}"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
                <Button Content="Simulate first" Click="Button1_OnClick"></Button>
                <Button Content="Simulate second" Click="Button2_OnClick"></Button>
            </StackPanel>
    

    代码

    private void Button1_OnClick(object sender, RoutedEventArgs e) {
                try {
                    NodeBarcode_1_GetFocusNow = false; // <--- Here
                    NodeBarcode_2_GetFocusNow = true;
    
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            private void Button2_OnClick(object sender, RoutedEventArgs e) {
                try {
                    NodeBarcode_1_GetFocusNow = true;
                    NodeBarcode_2_GetFocusNow = false; // <--- Here
    
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
    

    这 2 个按钮现在可以正确切换焦点。如果这不起作用,您可能忘记了一些事情要告诉。

    【讨论】:

    • 我已经尝试过这种解决方案,但它不起作用。焦点从“Nodebarcode_1”转到“NodeBarcode_2”,但不会回到 1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    相关资源
    最近更新 更多