question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TextBox, ComboBox, etc binding issues

See original GitHub issue

After update from 3.6.1.236 to 5.0.0.8 a lot of code has been broken Some samples that works perfect on 3.6.1.236 and not work on 5.0.0.8

In this sample binding to Text doesn’t work

<fluent:TextBox SizeDefinition="Small"
                                            Width="40"
                                            Name="FontSizeTextBox"
                                            Text="{Binding Font.Size, Mode=TwoWay}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="TextChanged">
                                        <i:InvokeCommandAction Command="{Binding FontSizeCommand}"
                                                               CommandParameter="{Binding Text, ElementName=FontSizeTextBox, Converter={StaticResource StringToIntConverter}}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </fluent:TextBox>

In this sample binding to SelectedValue doesn’t work

<fluent:ComboBox Name="FontFamilyCombobox"
                                             SizeDefinition="Small"
                                             Width="110"
                                             MaxDropDownHeight="500"
                                             Style="{StaticResource SystemFontCombobox}"
                                             SelectedValuePath="Source"
                                             SelectedValue="{Binding Font.Family, Mode=OneWay}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="SelectionChanged">
                                        <i:InvokeCommandAction Command="{Binding FontFamilyCommand}"
                                                               CommandParameter="{Binding ElementName=FontFamilyCombobox, Path=SelectedItem.Source}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </fluent:ComboBox>

Environment

  • Fluent.Ribbon v5.0.0.8
  • Windows Server 2012R2
  • .NET Framework 4.6.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:41 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
batzencommented, May 12, 2017

Ok, now i found out why the behavior changed. In version 3.6 the TextBox in Fluent.Ribbon inherited from RibbonControl and contained a TextBox. Somewhere between that version and version 5.0 the inheritance changed from RibbonControl to TextBox, thus no longer containing a separate TextBox. This change then reveals the bug in System.Windows.Interactivity.

You can use the following code, which is a fixed/modified version of InovkeCommandAction from System.Windows.Interactivity:

public sealed class InvokeCommandActionEx : TriggerAction<DependencyObject>
{
    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(InvokeCommandActionEx), (PropertyMetadata)null);
    public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(InvokeCommandActionEx), (PropertyMetadata)null);
    private string commandName;

    public string CommandName
    {
        get
        {
            this.ReadPreamble();
            return this.commandName;
        }
        set
        {
            if (!(this.CommandName != value))
                return;
            this.WritePreamble();
            this.commandName = value;
            this.WritePostscript();
        }
    }

    public ICommand Command
    {
        get
        {
            return (ICommand)this.GetValue(InvokeCommandAction.CommandProperty);
        }
        set
        {
            this.SetValue(InvokeCommandAction.CommandProperty, (object)value);
        }
    }

    public object CommandParameter
    {
        get
        {
            return this.GetValue(InvokeCommandAction.CommandParameterProperty);
        }
        set
        {
            this.SetValue(InvokeCommandAction.CommandParameterProperty, value);
        }
    }

    protected override void Invoke(object parameter)
    {
        if (this.AssociatedObject == null)
            return;
        ICommand command = this.ResolveCommand();

        BindingOperations.GetBindingExpression(this.AssociatedObject, InvokeCommandAction.CommandParameterProperty)?.UpdateTarget();

        if (command == null || !command.CanExecute(this.CommandParameter))
            return;
        command.Execute(this.CommandParameter);
    }

    private ICommand ResolveCommand()
    {
        ICommand command = (ICommand)null;
        if (this.Command != null)
            command = this.Command;
        else if (this.AssociatedObject != null)
        {
            foreach (PropertyInfo property in this.AssociatedObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (typeof(ICommand).IsAssignableFrom(property.PropertyType) && string.Equals(property.Name, this.CommandName, StringComparison.Ordinal))
                    command = (ICommand)property.GetValue((object)this.AssociatedObject, (object[])null);
            }
        }
        return command;
    }
}

The line that solves your issue is:

BindingOperations.GetBindingExpression(this.AssociatedObject, InvokeCommandAction.CommandParameterProperty)?.UpdateTarget();
1reaction
User3290commented, May 11, 2017

@punker76 we can’t know about view elements in view model. Generally we don’t speak about MVVM 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Can I force an update to a Textbox DataContext bound ...
When a user issues an Undo Command and the item to be undone is the textual changes of a text box, I want...
Read more >
data not binding to controls like TextBox,Combobox...etc... ...
I am not able to bind the data to controls like TextBox,Combobox...etc... inside ASPxPopupControl while PerformCallback from javascript ...
Read more >
combo box problem
I have a table with many records in fields Industry, State, Contract, Title, First_Name, Last_Name, etc. i filled the values of contract in ......
Read more >
Bind a linq query to a combo box's text only, without ...
It seems to be only the comboboxes that cause a record change. The text boxes, and Trackbars seem to behave normally. My question...
Read more >
Check if Textbox Text in a ComboBox - Forums
What I want to do is check to see if the value of the bound textbox of Type is one of the types...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found