Setting value in FontFamilyComboBox

Ribbon for WPF Forum

Posted 16 years ago by Steven S.
Version: 3.0.0405
Avatar
I have a question regarding how to properly update the value that is set in the FontFamilyComboBox. For example, I've noticed that the DocumentEditor Demo has the correct value set in the combo box when the app is started. However, I'm not sure how or where that is getting set. At first I thought that it was in the RichTextBoxExtended class where the CanExecute command is handled, however when i put simliar code to set the UpdateValue nothing happened for me.

Code from RichTextBoxExtended.cs
private void OnFontFamilyCanExecute(object sender, CanExecuteRoutedEventArgs e) {
    FontFamilyValueCommandParameter parameter = e.Parameter as FontFamilyValueCommandParameter;
    if ((parameter != null) && (!this.IsPreviewModeActive)) {
        parameter.UpdatedValue = this.SelectionFontFamily;
        parameter.Handled = true;
    }
    e.CanExecute = true;
}
For now I have written the following extension method that will do the trick, however it doesn't seem like the correct way to set the value. Can you provide an example on how you would 'hook up' the selected value for the FontFamilyComboBox?

My extension method
public static bool SetFontComboBoxValue(this FontFamilyComboBox comboBox, string font)
{
    int i = 0;
    foreach (ComboBoxItem item in comboBox.Items)
    {
        if (((FontFamily)item.Content).Source == font)
        {
            break;
        }
        i++;
    }

    bool foundValue = i < comboBox.Items.Count;
    if (foundValue)
    {
        comboBox.SelectedIndex = i;
    }
    return foundValue;
}
[Modified at 01/13/2008 12:08 AM]

Comments (7)

Posted 16 years ago by Shelley Bazinet
Avatar
I am having the same problem with FontFamilyComboBox.

Is there a better solution than the one provided here?
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Steven,

I apologize, I don't believe we ever got a notification email of this post for some reason. Otherwise we would have responded a long time ago.

Anyhow in response to the original post, if you aren't seeing the initial value get set, maybe try calling CommandManager.InvalidateRequerySuggested(). That should trigger the CanExecute.

Also your code is the correct way to set the item since the items are really ComboBoxItems with a Content of a FontFamily.

That being said, we've made a couple changes for the next release:

1) FontFamilyComboBox.GetFontFamily (static) - Takes a string name and returns the corresponding FontFamily.
2) Changed FontFamilyComboBox.SelectedValue to get/set the FontFamily of the selected item instead of returning the ComboBoxItem wrapper.

So with that change you'll be able to do things like this:
myFontFamilyComboBox.SelectedValue = FontFamilyComboBox.GetFontFamily("Courier New");


Actipro Software Support

Posted 15 years ago by Mike Turner
Avatar
I am running into the same issue as the original poster. In my CanExecuteFontNameCommand handler, I am casting the routed event argument to a FontFamilyValueCommandParameter and setting its UpdatedValue property to a particular FontFamily based on current selection in my view. The following is application specific but represents what I'm trying to do:

private void CanExecuteFontNameCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ExecutionUtility.CanExecuteFontNameCommand(_flowchart.Selection);

            if (e.CanExecute)
            {
                FontFamilyValueCommandParameter parameter = e.Parameter as FontFamilyValueCommandParameter;
                string fontFamily = ExecutionUtility.GetFontName(_flowchart.Selection);
                if (parameter != null && !string.IsNullOrEmpty(fontFamily))
                {
                    parameter.UpdatedValue = ActiproSoftware.Windows.Controls.Ribbon.Controls.FontFamilyComboBox.GetFontFamily(fontFamily);
                    e.Handled = true;
                }
            }
        }
Unfortunately, this does not update the selected value in the font family combobox. Does anything jump out as blatantly wrong? Suggestions?

Thanks for any help.
Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Make sure you set parameter.Handled = true too. Otherwise the control won't update its value. I think that is what you are missing.


Actipro Software Support

Posted 15 years ago by Mike Turner
Avatar
Thanks! Sometimes you stare at the code but cease to see. ;-)
Posted 12 years ago by joe sweeney - Technology Officer, TyTags Australia
Avatar

Hi,

 

I'm having a problem also with FontFamilyComboBox but mine is on xaml side and the page is a control, i want to use the SelectedItem Property to set the selected item in FontFamilyComboBox but it is not working, i also tried applying Mode=TwoWay.

xaml

<ribbon:FontFamilyComboBox Name="FontFamilySelector" Width="160" ScreenTipDescription="Change the font face." ScreenTipHeader="Font Style" SelectedItem="{Binding SelectedFontFamily}"/>

xaml.cs

 

public FontFamily SelectedFontFamily {
get { return (FontFamily)GetValue(SelectedFontFamilyProperty); }

set { SetValue(SelectedFontFamilyProperty, value); }
}

 

public static readonly DependencyProperty SelectedFontFamilyProperty =
DependencyProperty.Register("SelectedFontFamily", typeof(FontFamily),
typeof(FontToolBar), new FrameworkPropertyMetadata(new FontFamily("Arial"),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(SelectedFontFamilyCallBack),
new CoerceValueCallback(SelectedFontFamilyCoerceCallback)));

------------------------------------------ For all your label, tag and signage needs.

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Joe,

I believe the items are added as ComboBox items by default.  So you'd want to get/set the SelectedValue property instead.  That property should get/set the currently selected FontFamily.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.