【发布时间】:2011-09-22 05:33:13
【问题描述】:
我正在编写一个 WPF 应用程序,并且我有一个文本框供用户输入用于视频播放的每秒帧数值。此文本框的值绑定到后面代码中的依赖属性(尝试像优秀的设计师一样遵循 MVVM)。我的问题是在外部更改 FPS 值时文本框不会自动更新。例如,用户可以使用滑块控制值。滑块正确更改了依赖项属性值,但文本框文本永远不会更新,当然,除非我使用 GetBindingExpression(..).UpdateTarget() 手动完成,这是我已经实现的等待更好的解决方案。有谁知道这是预期功能还是我设置错误?
谢谢, 最大
XAML 中的文本框标记:
<TextBox Text="{Binding FPS}" Name="tbFPS" FlowDirection="RightToLeft"/>
依赖属性的代码:
#region public dependency property int FPS
public static readonly DependencyProperty FPSProperty =
DependencyProperty.Register("FPSProperty", typeof(int), typeof(GlobalSettings),
new PropertyMetadata(MainWindow.appState.gSettings.fps,FPSChanged,FPSCoerce),
FPSValidate);
public int FPS
{
get { return (int)GetValue(FPSProperty); }
set { SetValue(FPSProperty, value); }
}
private static bool FPSValidate(object value)
{
return true;
}
private static object FPSCoerce(DependencyObject obj, object o)
{
return o;
}
private static void FPSChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
//why do i need to update the binding manually? isnt that the point of a binding?
//
(obj as GlobalSettings).tbFPS.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
}
#endregion
【问题讨论】:
标签: c# wpf textbox binding dependency-properties