- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 UserControl 中有一个 TextBox,它绑定(bind)到 MainWindow 的 ViewModel 中的一个属性。
现在,当我在文本框中键入内容时,它会更新 View 模型中的属性,但如果我在后面的代码中更改文本框的文本, View 模型属性不会更新。
实际上,文本框是从我单击按钮时打开的 FileDialog 中获取值的,因此文本框是从后面的代码中获取其文本的。
用户控件 XAML:
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Left">
<TextBox x:Name="TextBoxFileOrFolder" Text="{Binding FolderOrFileName}" Grid.Row="1" Width="200" Height="100" HorizontalAlignment="Left"></TextBox>
<Button x:Name="ButtonRun" Content="Run" Click="ButtonRun_OnClick" Width="200" Height="100" Margin="10"></Button>
</StackPanel>
背后的UserControl代码
private void ButtonRun_OnClick(object sender, RoutedEventArgs e)
{
TextBoxFileOrFolder.Text = "FileName" + new Random().Next();
}
View 模型:
public class MainViewModel: INotifyPropertyChanged
{
public MainViewModel()
{ }
private string folderOrFileName;
public string FolderOrFileName
{
get { return folderOrFileName; }
set
{
if (folderOrFileName!=value)
{
folderOrFileName = value;
RaisePropertyChanged();
}
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the property changed.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
# endregion
}
最佳答案
but if I change Textbox's text in code behind, the viewmodel property isn't updating.
那是因为如果您在代码隐藏中设置文本框的 Text
属性,您将覆盖绑定(bind)。因此,当您更新 View 时,指向您的 View 模型的链接已经消失,因此没有任何东西可以更新它。而且,当 View 模型更新值时, View 也不会更新。
要解决这个问题,只需不要在代码隐藏中设置具有绑定(bind)的属性。
与其在代码隐藏中处理按钮事件并更新 View ,不如将按钮命令绑定(bind)到 View 模型并更新 View 模型中的 FolderOrFileName
。
关于c# - 更改 TextBox 文本时 ViewModel 属性未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32081153/
这个问题在这里已经有了答案: How to automatically select all text on focus in WPF TextBox? (33 个答案) How to select
我在我的项目中使用了以下模板:
我有一个非常奇怪的问题。我有多个用于用户名/密码的文本框,每个用户/密码组旁边都有一个复选框。当用户单击 CheckBox 时,如果他选中它,它旁边的用户名和密码 TextBox 将被启用,焦点将设置
我有两个带有这样的日期选择器的文本框。 $(function () { $('#value_date').datetimepicker({ format: 'MM/DD/YY
我检查了大约 60 个国家/地区的邮政编码。用户输入邮政编码,根据该国家/地区的规则进行检查。美国是个异常(exception),我可以根据已输入的其他街道地址信息自动填写值。在某些方面,它基本上是一
我们的应用程序启动几个后台进程并将它们的输出放入 TextBoxes - 每个在 TabControl 中的一个单独的 TabItem。我希望 TextBoxes 自动滚动以显示最后一个输出行,因此在
TextBlock 元素很好地处理了 LineHeight,允许文本完全显示,而无需剪切。但是,我想将其更改为 TextBox 以方便编辑文本,这就是我的麻烦开始的地方。 TextBlock 显示如下
我正在尝试“帮助”用户表单上文本输入框的用户输入百分比。 截至目前,我的输入框将任何输入转换为百分比形式。这意味着键入“6”会将值转换为 600%。如下所示 databaseViewer 是输入框从中
我有一个带有 RichEditBox 的 Windows 应用商店应用程序(编辑)和一个 Grid (边注)。 我需要始终匹配两个元素的垂直滚动位置。这样做的目的是允许用户在文档的页边空白处添加注释。
我有一个问题。下面的代码用于通过 .append() 和 .remove() 动态添加和删除文本框。我希望占位符为 textbox 的文本框中的所有数据都会内爆,并放置在我设置的名称为 textbox
我有一个文本框,其 Text 属性具有 TwoWay MultiBinding,UpdateSourceTrigger 设置为 属性已更改。第一个 Binding 是一个依赖属性 (Value),它具
它们之间有什么区别吗?谢谢 TextBox.Clear(); TextBox.Text = string.Empty; 最佳答案 在实践中:不。在内部,有。两者都以完全不同的方式清除文本。 我不敢告
这两种方法有什么区别? 一个比另一个更有效率吗? 我在想 AppendText() 可能使用类似于 StringBuilder 的方法,即它使用自己的缓存而不是每次都创建和附加一个新字符串,是这样吗?
我有一个文本框来显示一个很长的 Base64 字符串。 TextBox.Multline = true 和 TextBox.WordWrap = true。 此问题是由文本框本身的自动字边界检测引起的
在 this document在描述 Windows 10 UWP 应用程序的生命周期时,它指出: Users now expect your app to remember its state as
我需要检查如果我的 TextBox (txtmoney) 为空或等于 0,则不允许按 0 键键盘或数字 0。 我该怎么做? 最佳答案 Before implementing please refer
我有一个显示一些 TextBoxes 的 ListView。对于每个 TextBox,我都会捕获一个 SelectionChanged 事件。 我的 XAML 文件看起来像这样:
当我单击(或双击)Access 表单的文本框时,我需要选择其中的所有文本。我尝试了以下代码,但没有成功: Me.txt_CompraPreco.SelStart = 0 Me.txt_CompraPr
我有一个 ListView(在更新面板中)绑定(bind)到一个 ObjectDataSource(连接到一个 NHibernate 对象)并且在 EditItemTemplate 我使用 Text=
我正在尝试使用 D3 绘制多条交互式线条。单击每一行时,应根据当前鼠标坐标显示一个 float 文本框,并等待输入更新行坐标。更改文本框后,该行应自动更新,文本框应消失。有人可以看看我的代码并找出问题
我是一名优秀的程序员,十分优秀!