Wpf Caliburn Micro:TextBox getter 没有 return 最新值
Wpf Caliburn Micro: TextBox getter doesn't return newest value
我有一个非常简单的示例代码,一个 TextBox 和一个 Button。用户在 textBox 中输入一些文本并单击 Button。它应该显示一个普通的 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
}
}
<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 上没有。唯一可聚焦的是在这方面您所做的代码片段的一部分是阻止键盘访问..
我有一个非常简单的示例代码,一个 TextBox 和一个 Button。用户在 textBox 中输入一些文本并单击 Button。它应该显示一个普通的 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
}
}
<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 上没有。唯一可聚焦的是在这方面您所做的代码片段的一部分是阻止键盘访问..