问题描述
我的目标是通过DependencyProperties操纵应用程序的文本样式。我得到了一个图表,在其中可以对文本进行大小,字体家族,颜色等操作。因此,我想使用类似于Word等富文本编辑器的界面。
My goal is to manipulate the text-styles of my application via DependencyProperties. I got a diagram in which the texts are to be manipulated in size, fontfamily, color, etc. So I'd like to use an interface similar to a rich text editor like Word.
我在我的TextStyleVM中使用此代码
I'm using this code in my TextStyleVM http://shevaspace.blogspot.com/2006/12/i-have-some-fun-with-formattedtext_14.html
有一个FontFamilyProperty和一个Getter和Setter:
So I have a FontFamilyProperty and a Getter and Setter for it:
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); }
}
然后有一个ToStyle方法,用于设置标签的样式
Then there is a ToStyle method, which sets the style for the labels of the diagram, which are to be manipulated:
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;
现在这适用于TextBox。文本框显示当前的FontFamily,如果我在文本框中输入新的有效的FontFamily(如Arial),则标签的FontFamily会更改。
Now this works for a TextBox. The textbox displays the current FontFamily, and if I enter a new, valid FontFamily like Arial into the textbox the FontFamily of the labels are changed.
但是,像是一个组合框,其中显示了SystemFonts,在这里我可以为标签选择一个FontFamily。但是,绑定似乎无效。既不显示系统字体也不显示标签的当前字体。组合框只是空的。
However, what I'd like to have is a combobox, which displays the SystemFonts and where I can choose one FontFamily for my labels. However, the binding doesn't seem to work. Neither the system fonts nor the current fonts of the labels are displayed. The combobox is just empty.
这是我的xaml:
<r:RibbonLabel Content="FontFamily" />
<!--these do not work-->
<r:RibbonComboBox SelectedItem="{Binding FontFamily}"/>
<r:RibbonComboBox ItemsSource="{Binding FontFamily}"/>
<!--this works-->
<r:RibbonTextBox Text="{Binding FontFamily}"/>
现在,我假设我必须在ToStyle方法中为ComboBox设置一个不同的Setter。但是我不知道,哪一个。也许像这样:
Now, I assume I have to set a different Setter for a ComboBox in the ToStyle Method. But I have no clue, which one. Maybe someting like this:
fontFamilySetter.Property = ComboBox.ItemSource;
但是,如果我设置了Property,则TextBox仍然有效。那么这是一个错误的起点吗?如果有人可以向我提示有关使用这些Style-,Setter-和Binding-key-word的文档(也将这些文档用于ToStyle方法中),我将不胜感激,因为这是我正在使用的其他代码。 / p>
However, if I set that Property, the TextBox still works. So is this the wrong place to start at? I'd also be grateful if someone could hint me to some documentation about using these Style-, Setter-, Binding-key-words, which are used in the ToStyle method, since this is somebody elses code I'm working with.
推荐答案
ItemsSource需要一个集合;例如Fonts.SystemFontFamilies
ItemsSource here expects a collection; e.g. Fonts.SystemFontFamilies
<ComboBox ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"/>
实际上,这是一个非常不错的涵盖字体选择的链接:
Actually, here's a very nice link covering font selection:
http://www.hanselman.com/blog/LearningWPFWithBabySmashCustomerFeedbackAndAWPFFontComboBox.aspx >
Scott Hanselman甚至展示了如何使用其自己的字体系列渲染组合框中的每个字体项目。
Scott Hanselman even shows how to render each font item in combobox with it's own font family.
为每个OP注释添加。
以下是绑定到依赖项属性的示例。该属性被命名为 MyFontFamily,以避免与现有Window的属性发生冲突。抱歉,没有功能区控件(我只有3.5 sp1)。
Here's an example of binding to dependency property. Property is named "MyFontFamily" to avoid collision with existing Window's property. Sorry, no Ribbon controls (I have bare 3.5 sp1).
Window1.xaml
Window1.xaml
<Window
x:Class="SimpleWpf.Window1"
x:Name="ThisWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Vertical">
<ComboBox
SelectedItem="{Binding MyFontFamily, ElementName=ThisWindow}"
ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"/>
<TextBlock
Text="Lazy dog jumper"
FontFamily="{Binding MyFontFamily, ElementName=ThisWindow}"
FontSize="24"/>
</StackPanel>
</Window>
Window1.xaml.cs
Window1.xaml.cs
public partial class Window1 : Window
{
// ...
public static readonly DependencyProperty MyFontFamilyProperty =
DependencyProperty.Register("MyFontFamily",
typeof(FontFamily), typeof(Window1), new UIPropertyMetadata(null));
}
这篇关于在组合框中显示FontFamily的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!