相关疑难解决方法(0)

为C#设计自定义字体对话框/选择器,过滤掉非真实类型的字体

由于内置字体对话框在选择非真实类型字体时返回"非真实类型字体"异常,我正在尝试使用字体系列创建自定义字体对话框,该字体系列会过滤掉非真实类型字体.

控件工作正常但我需要这个对话框的大小和样式选择器.我发布了当前的代码.请帮我添加尺寸和样式选择器.它也可能对你有用.

public class FontListBox : ListBox
{
    private List<Font> _fonts = new List<Font>();
    private Brush _foreBrush;

    public FontListBox()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
        ItemHeight = 20;
        foreach (FontFamily ff in FontFamily.Families)
        {
            // determine the first available style, as all fonts don't support all styles
            FontStyle? availableStyle = null;
            foreach (FontStyle style in Enum.GetValues(typeof(FontStyle)))
            {
                if (ff.IsStyleAvailable(style))
                {
                    availableStyle = style;
                    break;
                }
            }

            if (availableStyle.HasValue)
            {
                Font font = null;
                try
                {
                    // do your own Font initialization here …
Run Code Online (Sandbox Code Playgroud)

.net c# fonts dialog winforms

8
推荐指数
1
解决办法
3266
查看次数

在Combobox中显示FontFamily

我的目标是通过DependencyProperties操作我的应用程序的文本样式.我得到了一个图表,其中的文本将被大小,字体家族,颜色等操纵.所以我想使用类似于Word等富文本编辑器的界面.

我在我的TextStyleVM http://shevaspace.blogspot.com/2006/12/i-have-some-fun-with-formattedtext_14.html中使用此代码

所以我有一个FontFamilyProperty和一个Getter和Setter:

        public static DependencyProperty FontFamilyProperty =
            DependencyProperty.Register(
                "FontFamily",
                typeof(FontFamily),
                typeof(OutlinedText),
                new FrameworkPropertyMetadata(
                   SystemFonts.MessageFontFamily,
                   FrameworkPropertyMetadataOptions.AffectsRender |
                   FrameworkPropertyMetadataOptions.AffectsMeasure),
                      new ValidateValueCallback(IsValidFontFamily)); 

  public FontFamily FontFamily
    {
        get { return (FontFamily)base.GetValue(FontFamilyProperty); }
        set { base.SetValue(FontFamilyProperty, value); }
    }
Run Code Online (Sandbox Code Playgroud)

然后有一个ToStyle方法,它设置图表标签的样式,这些样式将被操作:

        Style style = new Style();
        Binding fontFamilyBinding = new Binding("FontFamily");
        fontFamilyBinding.Source = this;
        Setter fontFamilySetter = new Setter();
        fontFamilySetter.Property = TextBlock.FontFamilyProperty;
        fontFamilySetter.Value = fontFamilyBinding;
        style.Setters.Add(fontFamilySetter);

        return style;
Run Code Online (Sandbox Code Playgroud)

现在这适用于TextBox.文本框显示当前的FontFamily,如果我在文本框中输入新的有效FontFamily(如Arial),则会更改标签的FontFamily.

但是,我想要的是一个组合框,它显示SystemFonts,我可以为我的标签选择一个FontFamily.但是,绑定似乎不起作用.既不显示系统字体也​​不显示标签的当前字体.组合框只是空的.

这是我的xaml:

            <r:RibbonLabel Content="FontFamily" />
            <!--these do not work-->
            <r:RibbonComboBox SelectedItem="{Binding FontFamily}"/>
            <r:RibbonComboBox ItemsSource="{Binding …
Run Code Online (Sandbox Code Playgroud)

wpf combobox dependency-properties attached-properties

6
推荐指数
2
解决办法
1万
查看次数