好的,所以我想子类ObservableCollection为它添加一个属性.不幸的是,PropertyChanged活动受到保护.基本上我想将它子类SelectedItem化为我可以绑定到我的MVVM WPF应用程序中的列表.
这是我班级的骨架:
public class SelectableList<T> : ObservableCollection<T>
{
public T SelectedItem {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
但我不能做到以下几点:
SelectableList<int> intList = new SelectableList<int>();
intList.PropertyChanged += new PropertyChangedEventHandler(intList_Changed);
Run Code Online (Sandbox Code Playgroud)
因为访问限制.这让我提出了一个更深层次的问题.为什么用户界面可以获得PropertyChanged事件通知(例如Count属性),我不能在代码隐藏中做到这一点?
我的头在旋转,有人可以赐教吗?
我正在创建一个WPF CustomControl,它具有PropertyChangedCallback的依赖属性.在那个Callback方法中,我尝试使用GetTemplateChild()方法设置我从OnApplyMethod检索的某些控件部分的值.
问题是PropertyChangedCallback(在某些系统上)在OnApplyTemplate之前调用,因此控件部分仍为空.
我目前使用的解决方法是将PropertyChangedCallback中的e.NewValue保存到成员变量,然后在OnApplyTemplate()中调用SetValue(dp,_savedValue).
处理这个问题的正确方法是什么,或者我已经使用了最佳解决方案?
通常,当您希望数据绑定控件"更新"时,使用"PropertyChanged"事件向接口发出数据在幕后发生更改的信号.
例如,您可以使用属性"DisplayText"绑定到datacontext的文本块
<TextBlock Text="{Binding Path=DisplayText}"/>
从这里,如果DataContext使用PropertyName"DisplayText"引发PropertyChanged事件,则此文本块的文本应该更新(假设您没有更改绑定的模式).
但是,我有一个更复杂的绑定,它使用datacontext的许多属性来确定控件的最终外观.为此,我直接绑定到datacontext并使用转换器.在这种情况下,我正在使用图像源.
<Image Source="{Binding Converter={StaticResource ImageConverter}}"/>
如您所见,我使用没有路径的{Binding}直接绑定到datacontext,我使用ImageConverter选择我正在寻找的图像.但现在我无法(我知道)告诉绑定更新.我试着用"."来提高propertychanged事件.作为属性名称,它不起作用.
这可能吗?我是否必须将转换逻辑包装到绑定可以附加到的属性中,或者有没有办法告诉绑定刷新(没有明确刷新绑定)?
任何帮助将不胜感激.谢谢!-亚当
我一直在争论这个问题一段时间似乎无法提出具体的解决方法 - 我有一个绑定到小数的TextBox,并且绑定将UpdateSourceTrigger设置为PropertyChanged并且必要时(如此)在这种情况下,LostFocus将无法正常工作).我确定的默认行为在某种程度上是可以解释的,对于我的目的是不可接受的,所以我尝试了以下的StringFormat,我曾经想过要解决这个问题,但只是部分解决,现在我正在寻找更具体的东西.我最初的解决方法是在绑定中添加一个字符串格式......就我的情况而言
StringFormat={0:#.#####}
Run Code Online (Sandbox Code Playgroud)
因此,当输入类似.12345或1.5的内容时,解决方案效果很好,但是如果我键入.01234,一旦我按下零键,它就会删除我刚刚输入的小数...这显然是一个很难的原因数据录入.我希望我对字符串格式的熟悉程度不足.Wost案例场景我将我的公开属性设为字符串,而setter和getter只是转换为十进制,但这似乎是一个hacky解决方案.
谢谢!
AJ
我想听一下JSpinner的SpinnerNumberModel值的变化.
我创建了一个PropertyChangeSupport并将模型放入其中.
我需要propertyChangeListener,因为它显示了属性的旧值和新值.
该代码段不起作用:propertyChange当我单击JSpinner时,该方法不会输出任何内容.
一个简单的ChangeListener只提供新值,但我还需要旧值,我怎样才能得到它?
package de.unikassel.jung;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
public class PropertyChangeTest implements PropertyChangeListener {
public static void main(String[] args) {
new PropertyChangeTest();
}
public PropertyChangeTest() {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int value = 1;
int min = 0;
int max = 10;
int step = 1;
SpinnerNumberModel spinnerModel = new SpinnerNumberModel(value, min, max, step);
PropertyChangeSupport pcs = new PropertyChangeSupport(spinnerModel);
pcs.addPropertyChangeListener("value", this); …Run Code Online (Sandbox Code Playgroud) java swing propertychanged propertychangesupport propertychangelistener
我在C#工作,我有一个我只能使用的对象Reflection(出于某些个人原因).所以,当我需要为其中一个属性设置一些值时,我会按如下方式执行:
System.Reflection.PropertyInfo property = this.Parent.GetType().GetProperty("SomeProperty");
object someValue = new object(); // Just for example
property.SetValue(this.Parent, someValue, null);
Run Code Online (Sandbox Code Playgroud)
并且,为了获得它的价值,我使用了这个方法GetValue.
我的问题是:当使用Reflection更改属性时,是否有办法触发事件?
文章http://msdn.microsoft.com/en-us/magazine/dd419663.aspx具有以下代码示例:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是通过引入变量'handler'获得了什么 - 以下代码似乎工作正常:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged!= null)
{
var e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道PropertyChanged当用户在输入文本时暂停时是否可以引发事件TextBox?或者更具体地说,我想X在用户停止在TextBox中键入数秒后运行一个方法.
例如,我有一个带有TextBox的表单,没有别的.用户在TextBox中键入1-9位Id值,相当资源密集的后台进程加载记录.
我不想使用,UpdateSouceTrigger=PropertyChanged因为这会导致资源密集型后台进程在键入字符时运行,因此9位ID号从这些进程中的9个开始.
我也不想使用,UpdateSourceTrigger=LostFocus因为表单上没有任何其他内容可以使TextBox失去焦点.
那么有没有办法让我的后台进程只有在用户输入ID号时暂停后才会运行?
我有以下文本框在viewmodel中具有propertychanged.当我插入Binding.ValidationRules并插入一些错误的值时,它不会触发propertychanged事件,我不明白为什么.有帮助吗?
<TextBox Name="RiskCode" HorizontalAlignment="Left" Margin="101.923,8,0,81" TextWrapping="Wrap" Width="56.077" MaxLength="6" Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="RiskCode" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<vm:RiskCodeValidation/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud) 我在Windows Phone Silverlight应用程序中使用MVVM Light.
我真的不知道RaisePropertyChanged应该如何工作; 让我用这样的代码解释一下
private Recipe _selectedRecipe;
public Recipe SelectedRecipe
{
get
{
return this._selectedRecipe;
}
set
{
this._selectedRecipe = value;
RaisePropertyChanged("SelectedRecipe");
}
}
Run Code Online (Sandbox Code Playgroud)
调用RaisePropertyChanged("SelectedRecipe")时会发生什么?
我希望调用一个新的方法来执行我的代码,或类似的东西,但我找不到类似的(少数)例子.那么,它是如何工作的?
propertychanged ×10
wpf ×6
c# ×4
mvvm ×3
textbox ×2
binding ×1
data-binding ×1
datacontext ×1
java ×1
mvvm-light ×1
reflection ×1
swing ×1
timer ×1
validation ×1