Tor*_*ten 6 wpf combobox dependency-properties attached-properties
我的目标是通过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 FontFamily}"/>
<!--this works-->
<r:RibbonTextBox Text="{Binding FontFamily}"/>
Run Code Online (Sandbox Code Playgroud)
现在,我假设我必须在ToStyle方法中为ComboBox设置不同的Setter.但我不知道哪一个.也许像这样:
fontFamilySetter.Property = ComboBox.ItemSource;
Run Code Online (Sandbox Code Playgroud)
但是,如果我设置该属性,TextBox仍然有效.这是错误的开始吗?如果有人可以提示我使用ToStyle方法中使用的Style-,Setter-,Binding-key-words这些文档,我也很感激,因为这是我正在使用的代码.
DK.*_*DK. 11
ItemsSource在这里需要一个集合; 例如Fonts.SystemFontFamilies
<ComboBox ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"/>
Run Code Online (Sandbox Code Playgroud)
实际上,这是一个非常好的链接,涵盖字体选择:
http://www.hanselman.com/blog/LearningWPFWithBabySmashCustomerFeedbackAndAWPFFontComboBox.aspx
Scott Hanselman甚至展示了如何使用它自己的字体系列渲染combobox中的每个字体项.
根据OP评论添加.
这是绑定到依赖属性的示例.属性名为"MyFontFamily"以避免与现有Window属性发生冲突.对不起,没有功能区控件(我有3.5 sp1).
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>
Run Code Online (Sandbox Code Playgroud)
Window1.xaml.cs
public partial class Window1 : Window
{
// ...
public static readonly DependencyProperty MyFontFamilyProperty =
DependencyProperty.Register("MyFontFamily",
typeof(FontFamily), typeof(Window1), new UIPropertyMetadata(null));
}
Run Code Online (Sandbox Code Playgroud)
A just in Xaml 解决方案,字体按字母顺序排序:
<Window x:Class="WpfFontsComboBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Height="350" Width="525">
<Window.Resources>
<CollectionViewSource x:Key="SortedFontsCollection" Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}" >
<CollectionViewSource.SortDescriptions>
<ComponentModel:SortDescription PropertyName="Source" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<StackPanel>
<Label Content="42" FontFamily="{Binding ElementName=comboBoxFonts, Path=SelectedItem}" />
<ComboBox x:Name="comboBoxFonts" ItemsSource="{Binding Source={StaticResource SortedFontsCollection}}" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10845 次 |
最近记录: |