This is a migrated thread and some comments may be shown as answers.

RadComboBox not setting bound property value on button click

1 Answer 170 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Jay asked on 11 Apr 2013, 01:07 AM
Hello, 
I'm running into a problem involving focus not being lost on an editable RadComboBox (in a property grid) when I click on a RadRibbonButton (in a RadRibbonGroup).  If I type some text into the combo box text area, it is not stored to the dependency property that it is bound to until the control loses focus.

<telerik:RadComboBox
   IsMouseWheelEnabled="False"
   ItemContainerStyle="{StaticResource ComboBoxItemStyle}"
   IsEditable="True"
   Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=LostFocus}">
   <telerik:RadComboBox.ItemsSource>
      <Binding Converter="{StaticResource AssetToListConverter}">
         <Binding.Path>Value</Binding.Path>
      </Binding>
   </telerik:RadComboBox.ItemsSource>
</telerik:RadComboBox>

This is required because the Value property is coerced to a valid string before being accepted, and without UpdateSourceTrigger=LostFocus the text box fails to take on the coerced value, something broken in the binding there.

public Object Value
{
   get
   {
      return _property.GetValue(_asset, null);   //using reflection
   }
   set
   {
      //SetValue ends up calling a coerce function which leaves the property untouched if value is invalid
      _property.SetValue(_asset, value, null);
      RaisePropertyChanged(() => Value);
   }
}

If UpdateSourceTrigger was set to PropertyChanged, this setter would be called every time the user types any characters.  If they type an invalid character the coerce function leaves the property unchanged but the text box displays the incorrect string, even though Value is different.

If the user starts typing something into the combo box and then clicks the button (which does a save or export), the Value is not set because the combo box does not lose focus.

<telerik:RadRibbonGroup x:Name="root" Header="Destructible" DialogLauncherVisibility="Collapsed">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <telerik:RadRibbonButton Grid.Row="0"
                                     Grid.Column="0"
                                     Text="New"
                                     IsAutoSize="True"
                                     Command="{Binding NewCommand}" />
        <telerik:RadRibbonButton Grid.Row="1"
                                     Grid.Column="0"
                                     Text="Save"
                                     IsAutoSize="True"
                                     Command="{Binding SaveCommand}" />
        <telerik:RadRibbonButton Grid.Row="2"
                                     Grid.Column="0"
                                     Text="Export"
                                     IsAutoSize="True"
                                     Command="{Binding ExportCommand}" />
    </Grid>
</telerik:RadRibbonGroup>

These two controls are in different assemblies.  I tried clearing focus for the scope in the button command code-behind but at that time Keyboard.FocusedElement is the button.  I can't find any way to force all focus scopes to clear their FocusedElement fields either.  I tried playing with the button's IsTabStop property.  I tried hooking up to a focus changed event on the button (can't find one that gives me the previously focused element, and it probably wouldn't be what I want thanks to focus scopes).  

I might be able to cache off the element every time something changes focus in the property grid and clear its focus scope on button clicks, but this doesn't seem very scaleable.

So I need to fix one of two problems.  Either figure out how to clear focus on the combo box when the button is clicked but before the command is executed, or figure out how to fix the binding issue of the text in the combo box.  Do you have any advice on either fronts?

TIA, Jay

1 Answer, 1 is accepted

Sort by
0
Zarko
Telerik team
answered on 15 Apr 2013, 11:29 AM
Hi Jay,
The problem is that the RibbonView is a different focus scope and that's why your comboBox still has focus. You could change this by setting the IsFocusScope property to false:
<telerik:RadRibbonView x:Name="ribbonView"
            FocusManager.IsFocusScope="False"
             ...>
Another workaround is to manually update the source on button click if you know the comboBox:
this.combo.GetBindingExpression(RadComboBox.TextProperty).UpdateSource()
I hope I was able to help you and if you have further questions please feel free to ask.

Kind regards,
Zarko
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
Buttons
Asked by
Jay
Top achievements
Rank 1
Answers by
Zarko
Telerik team
Share this question
or