3

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

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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);

2 Comments

Well it seems like its working fine after I implemented the suggestion by @CodeNaked I thought two way binding was the default anyway
If it's working, great. I just know that I've run into problems before where the option was not specified in the registration and my binding failed unless I set the mode explicitly. Maybe this is a change in 4.0?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.