开发者

WPF Binding to textbox not updating

开发者 https://www.devze.com 2023-03-15 17:59 出处:网络
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 be

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,FPSCoer开发者_Go百科ce),
        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


Not sure if this is the issue, but you should pass "FPS" as the property name, not "FPSProperty", like so:

public static readonly DependencyProperty FPSProperty =
    DependencyProperty.Register("FPS", typeof(int), typeof(GlobalSettings),
    new PropertyMetadata(MainWindow.appState.gSettings.fps,FPSChanged,FPSCoerce),
    FPSValidate);


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:

public static readonly DependencyProperty FPSProperty =
    DependencyProperty.Register("FPS", typeof(int), typeof(GlobalSettings),
    new FrameworkPropertyMetadata(MainWindow.appState.gSettings.fps, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, FPSChanged, FPSCoerce),
    FPSValidate);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号