【问题标题】:Wpf Caliburn Micro: TextBox getter doesn't return newest valueWpf Caliburn Micro:TextBox getter 不返回最新值
【发布时间】:2015-09-11 08:40:45
【问题描述】:

我有一个非常简单的示例代码,一个文本框和一个按钮。用户在 textBox 中输入一些文本并单击按钮。它应该显示一个普通的 MessageBox,其中包含他刚刚在 TextBox 中输入的文本。

问题:MessageBox 显示空白!经过反复试验,我找到了原因。我在 .xaml 中将 Button 的 Focusable 设置为 false。为什么?因为我想在单击一次后停止按钮的闪烁效果。

所以我有一个想法。我将 Focusable 绑定到 ViewModel 中的一个属性,并在 ViewModel 的构造函数(或作为初始值的私有字段)中初始化为 false。然后在显示 MessageBox 之前的 Button-Click 事件处理程序中,我将属性设置为 true,然后在执行 MessageBox 后再次将其设置为 false。 问题:很遗憾,它不起作用!似乎 Focusable 只能在构造函数或私有字段中设置一次作为初始值。

有人知道如何解决这个问题吗?我想将 Button 的 Focusable 保持为 false(以避免 Button 闪烁),并且我想通过单击 Button 从 TextBox 获取文本。以下是示例代码,您可以随意修改并向我展示解决方案。提前谢谢你。

XAML/视图

<Grid Width="300" Height="300">
    <StackPanel VerticalAlignment="Center">
        <TextBox Width="150" Text="{Binding Path=MyText}" />
        <Button x:Name="ShowText"
                Width="100"
                Content="Show Text"
                Focusable="{Binding Path=MyFocus}" />
    </StackPanel>
</Grid>

查看模型

public class ShellViewModel : PropertyChangedBase 
{
    private String _myText;
    public String MyText
    {
        get { return _myText; }
        set
        {
            _myText = value;
            NotifyOfPropertyChange(() => MyText);
        }
    }

    private Boolean _myFocus = false; // if it is true, the MessageBox can show MyText, otherwise MessageBox shows blank
    public Boolean MyFocus
    {
        get { return _myFocus; }
        set
        {
            _myFocus = value;
            NotifyOfPropertyChange(() => MyFocus);
        }
    }

    public void ShowText()
    {
        //MyFocus = true; // this doesn't work
        MessageBox.Show(MyText);
        //MyFocus = false; // this doesn't work
    }
}

【问题讨论】:

    标签: wpf button textbox caliburn.micro focusable


    【解决方案1】:
    <Grid Width="300" Height="300">
         <StackPanel VerticalAlignment="Center">
            <TextBox Width="150" Text="{Binding MyText, Mode=TwoWay}" />
             <Button x:Name="ShowText"
                Width="100"
                Content="Show Text"
                Focusable="{Binding MyFocus}" />
        </StackPanel> 
    </Grid>
    

    <Grid Width="300" Height="300">
        <StackPanel VerticalAlignment="Center">
           <TextBox Width="150" x:Name="MyText" />
           <Button x:Name="ShowText"
                Width="100"
                Content="Show Text"
                Focusable="{Binding MyFocus}" />
        </StackPanel>
    </Grid>
    

    无论哪种方式都应该起作用,您进行绑定的方式永远不会更新基础属性,因为它不被认为是双向的。因此,第一个版本需要 Mode=TwoWay,第二个版本使用 CM 的约定来查找文本框并将其绑定到视图模型上的同名属性。

    不确定您所指的默认样式是什么闪烁没有闪烁...至少在 Windows 8.1 或 Windows 10 上没有。就您所做的代码片段而言,唯一可聚焦的事情是阻止键盘访问..

    【讨论】:

    • 我指的是这个闪烁/闪烁问题stackoverflow.com/questions/18244605/… ...在您告诉我之前,我没有注意到在 WIndows 8.x 或更高版本上不会发生闪烁问题。我的“旧”电脑使用的是 Win7,我遇到了这样的闪烁按钮问题。谢谢你的回答。
    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多