WPF 绑定到文本框未更新
我正在编写一个 WPF 应用程序,并且有一个文本框供用户输入视频播放的每秒帧数值。该文本框的值绑定到后面代码中的依赖属性(尝试像优秀设计师一样遵循 MVVM)。我的问题是,当外部更改 FPS 值时,文本框不会自动更新。例如,用户可以使用滑块控制该值。滑块可以正确更改依赖项属性值,但文本框文本永远不会更新,除非我使用 GetBindingExpression(..).UpdateTarget() 手动执行此操作,这是我在等待更好的解决方案之前实现的。有谁知道这是预期的功能还是我设置错误?
谢谢, XAML 中的Max
TextBox 标记:
<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
I have am writing a WPF application, and I have a textbox for the user to enter a frames per second value for video playback. The value of this textbox is bound to a dependency property in the code behind (trying to follow MVVM like a good designer). My problem is that the textbox is not updating automatically when the FPS value is changed externally. For example, the user can control the value using a slider. The dependency properties value is changed correctly by the slider, but the textboxes text never updates, unless, of course, i do it manualy using GetBindingExpression(..).UpdateTarget() which is what I have implemented pending a better solution. Does anyone know if this is intended functionality or am I setting something up wrong?
Thanks,
Max
TextBox tag in XAML:
<TextBox Text="{Binding FPS}" Name="tbFPS" FlowDirection="RightToLeft"/>
Code behind for dependency property:
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否是问题,但您应该传递“FPS”作为属性名称,而不是“FPSProperty”,如下所示:
Not sure if this is the issue, but you should pass "FPS" as the property name, not "FPSProperty", like so:
我还认为您需要将 FrameworkPropertyMetadataOptions.BindsToWayByDefault 添加到依赖项属性注册中,否则您需要手动将 TextBox.Text 绑定上的模式设置为 TwoWay。
要使用 FrameworkPropertyMetadataOptions,您需要在注册中使用 FrameworkPropertyMetaData 而不是 PropertyMetadata:
I also think you need to add the FrameworkPropertyMetadataOptions.BindsToWayByDefault to your dependency property registration otherwise you need to manually set the mode on your TextBox.Text binding to TwoWay.
To use the FrameworkPropertyMetadataOptions, you need to use FrameworkPropertyMetaData instead of PropertyMetadata in your registration: