- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将可观察集合绑定(bind)到用户控件,但它不会根据用户更改进行更新,但会在通过代码更改用户控件时进行更新。以下是我试过的一个例子。它可能有点长,但它正在运行,因此您可以按原样复制和粘贴代码。
请在帖子末尾查看我的问题。
--Customer.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;namespace TestMVVM{ class Customer : INotifyPropertyChanged { private string firstName; private string lastName; public string FirstName { get { return firstName; } set { if (firstName != value) { firstName = value; RaisePropertyChanged("FirstName"); } } } public string LastName { get { return lastName; } set { if (lastName != value) { lastName = value; RaisePropertyChanged("LastName"); } } } #region PropertChanged Block public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } #endregion }}
--UCTextBox.xaml
<UserControl x:Class="TestMVVM.UCTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="40" Width="200">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="txtTextControl"
VerticalAlignment="Top" Width="120" />
</Grid>
--UCTextBox.xaml.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Collections.ObjectModel;using System.ComponentModel;namespace TestMVVM{ /// /// Interaction logic for UCTextBox.xaml /// public partial class UCTextBox : UserControl, INotifyPropertyChanged { public UCTextBox() { InitializeComponent(); } public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UCTextBox), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args) { UCTextBox pasTextBox = (UCTextBox)property; pasTextBox.txtTextControl.Text = (string)args.NewValue; } public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); NotifyPropertyChanged("Text"); } } private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion }}
-- Window1.xaml
<Window x:Class="TestMVVM.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestMVVM"
Title="Window1" Height="300" Width="300">
<Grid>
<local:UCTextBox x:Name="txtUC" />
<Button Height="23" HorizontalAlignment="Left" Margin="39,0,0,82"
Name="btnUpdate" VerticalAlignment="Bottom" Width="75" Click="btnUpdate_Click">Update</Button>
<Button Height="23" Margin="120,0,83,82" Name="btnChange" VerticalAlignment="Bottom" Click="btnChange_Click">Change</Button>
</Grid>
-- Window1.xaml.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Collections.ObjectModel;namespace TestMVVM{ /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window { CustomerHeaderViewModel customerHeaderViewModel = null; public Window1() { InitializeComponent(); customerHeaderViewModel = new CustomerHeaderViewModel(); customerHeaderViewModel.LoadCustomers(); txtUC.DataContext = customerHeaderViewModel.Customers[0]; Binding binding = new Binding(); binding.Source = customerHeaderViewModel.Customers[0]; binding.Path = new System.Windows.PropertyPath("FirstName"); binding.Mode = BindingMode.TwoWay; binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; txtUC.SetBinding(UCTextBox.TextProperty, binding); } private void btnUpdate_Click(object sender, RoutedEventArgs e) { MessageBox.Show(customerHeaderViewModel.Customers[0].FirstName); } private void btnChange_Click(object sender, RoutedEventArgs e) { txtUC.Text = "Tom"; } } class CustomerHeaderViewModel { public ObservableCollection Customers { get; set; } public void LoadCustomers() { ObservableCollection customers = new ObservableCollection(); customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 }); Customers = customers; } }}
当我运行 Window1.xaml 时,我的用户控件将数据显示为“Jim”。现在,当我将文本更改为“John”并单击“更新”时,消息框仍然显示“Jim”,这意味着可观察集合没有得到更新。当我单击“更改”按钮时,用户控件将数据更改为“Tom”。现在,当我单击“更新”按钮时,消息框显示“Tom”。谁能告诉我如何通过更改用户控件中的数据而不是通过代码来实现可观察集合的更新?
最佳答案
那是因为您没有处理 txtTextControl.TextChanged
事件,所以您的 Text
依赖属性永远不会更新。
无论如何,您不需要使用 DependencyPropertyChangedCallback
和事件处理程序手动处理,您只需将 txtTextControl.Text
绑定(bind)到 Text
依赖属性:
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="txtTextControl"
VerticalAlignment="Top" Width="120"
Text="{Binding Path=Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:UCTextBox}}}"/>
关于wpf - 可观察集合未在 UI 更改时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3521775/
我正在尝试使用 tkinter 创建一个类似点击器的游戏作为练习。我对 tkinter 很陌生,所以如果问题非常基本,我深表歉意。我设置了一个按钮来添加点击次数,并且我还尝试设置自动点击功能。我的代码
我想以特定方式更新表 A 中的第 1 列:当列中的第三个字符是“_”时,我想插入前 2 个字符,如果第三个字符还有其他任何内容,我想保留它是。 例子: |col1|
我用 View 模型组装了一个简单的登录 fragment 。这是 fragment : class LoginFragment : Fragment() { companion object {
是否可以在 mySQL 中的创建表语句中编写更新语句?假设我们有两个不同的表。当我在一个表上插入某些内容时,我想更改另一表中的值。这在 mySQL 中可能吗 最佳答案 需要更多规范。 如果您有 2 个
我组合了一个简单的发布/订阅模式,以允许动态实例化和运行多个 JavaFX 类。这些类中的每一个(“订阅者”)都旨在可视化来自具有“发布者”角色的模型(在模型/ View / Controller 意
我正在使用 pygame 并在主循环的每个循环中更新到屏幕。我不明白的是,在我添加一个 for 循环查找事件之前,什么都不会更新,然后突然所有更新都发生了。这是为什么? def run(self
我是 React 的初学者;我知道 setState 是异步的,但我不明白为什么在示例笔中下方框下方的刷新不会立即完成,而仅在输入第二个字符后才更新。 Codepen:(已更新以链接到正确的笔) ht
我在 Java 程序中有两个选项卡。一个用于添加股票,另一个用于列出我创建的股票。当我在第二个选项卡中添加新项目时,我试图设法更新第一个选项卡中的项目列表。有任何想法吗? 我希望第一个选项卡显示我在第
我有一个 Activity A。加载 A 后,单击 A 中的按钮会在 A 的主页布局上添加 fragment F。现在一旦进入 F,如果我正在调用 getActivity().getSupportFr
下面提供的这段代码中的 friend 们,我想在从播放 Intent 恢复时刷新我的 TextView 。但是每当我尝试在 OnCreate 之外但在我的主类中定义我的 textview 时(在 st
我是 Postgres 的新手。我正在尝试使用 java/postgres jdbc 执行以下代码(适用于 MySQL): for (int i = 0; i < arr.size(); i++) {
目前,我有一个更新函数,可以更新一行,但如果我将其中一个框留空,而不是不更改该值,则该值将被删除。如果我只需要更新其中一个值,我想更新该值并将其他框留空。这可能吗? 目前我的代码是这样的。 最佳答案
我正在编写 JavaScript,它正在为一个项目计数到一定数量。数量可能在 100,000 左右,完成处理大约需要 10-15 秒。我希望脚本在用户调用页面时立即运行,并在脚本完成时进行重定向。是否
每当具有不同输入 ID 的另一个 selectInput 的值发生变化时,我需要更改一个具有自己输入 ID 的 selectInput 的值。 在 javascript 中,这将是 onchage,但
我正在尝试弄清楚如何在将视频上传到服务器时更新我的 UIProgressView。该视频是用户选择的视频,这是我上传到我的服务器的代码: NSMutableArray *array = [[NSM
我想在单击 h:commandButton 时更新 id="contents"的 div,但复杂的部分是 h:commandButton 位于 c:forEach Click Me
我目前正在构建一个读取数组并将其显示在 TableView 上的 UITableView。我不断地添加到数组中,我希望在 View 上有一个按钮,一旦单击它就会更新 UITableView。 如何从我
我在 StructuredProperty 中有一个 ComputedProperty,它在首次创建对象时不会更新。 当我创建对象时 address_components_ascii 没有被保存。该字
我知道 SCNPhysicsBody 在节点缩放时不会缩放,但我还没有找到解决此问题的好方法。我想缩放节点,然后在缩放后将 SCNPhysicsBody 更新为节点。 let box = SCNBox
我在自定义 UITableViewCell 中有一个 UILabel,当设备旋转时,它会调整大小。旋转后需要重新计算此标签中的文本,因为我要将其缩小到适当大小并在末尾附加一些文本。 例如数据模型有:“
我是一名优秀的程序员,十分优秀!