Show Font-Style in a Combobox in it's own style

Khushi

I have two comboboxes.

  1. for FontFamily.
  2. for FontWeight.

Xaml for 1st Combobox looks like:

<ComboBox IsEditable="True"
          ItemsSource="{x:Static Fonts.SystemFontFamilies}" >
    <ComboBox.ItemTemplate>
        <DataTemplate DataType="{x:Type FontFamily}">
            <TextBlock Text="{Binding}" FontFamily="{Binding}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

In the output of the above XAML I can see all the font-names in its own style. I want to do something similar for 2nd combobox also. Currently I have some items in 2nd combobox as below:

<ComboBox IsEditable="True">
    <x:Static Member="FontStyles.Normal"/>
    <x:Static Member="FontStyles.Italic"/>
    <x:Static Member="FontStyles.Oblique"/>
</ComboBox>

How can I show each item in above combobox in its own style using Combobox.ItemTemplate or something similar without styling each item.

For e.g. my output should look something like:

Normal

Italic

Oblique

XAMeLi

Take advantage of type converters: for most properties there's a converter that converts a string to a suitable value for the property. It's needed to be able to parse XAML (which is all strings) to types (think of writing something like Width="Auto" while remembering that Width is a double value).

So, you can use something like this:

<ComboBox>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding }"
                        FontStyle="{Binding }" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <system:String>Normal</system:String>
    <system:String>Italic</system:String>
    <system:String>Oblique</system:String>
</ComboBox>

The binding for FontStyle sets a string and then type converter springs into action and converts the string to an actual FontStyle value to be used by the property.

NOTE: this might not work in .NET 3.0/3.5

EDIT : just remembered, in .NET 3.0/3.5, if there's a converter defined for the binding, then type converter is not working - the binding expects the converter to return the right type for the property. Not sure if it was changed in .NET 4.0/4.5 (probably not, and ,IMHO, shouldn't have - need to check it to verify).

Oh, and add this xmlns: xmlns:system="clr-namespace:System;assembly=mscorlib"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

changing the font style of HTML table?

来自分类Dev

WPF:ComboBox:在Style / ControlTemplate中公开DataTemplate绑定

来自分类Dev

SICP, Continuation Passing Style and Clojure's trampoline

来自分类Dev

Tinymce fontsizeselect未使用“ theme_advanced_font_sizes”或“ font_size_style_values”值

来自分类Dev

如何正确声明带有font-weight和font-style的CSS字体?

来自分类Dev

结合font-style:italic和font-weight:bold导致FF中的反向倾斜

来自分类Dev

不建议使用的代码:<b> vs style =“ font-weight:bold;”

来自分类Dev

如何在更改YouTube视频时使用ng-hide / ng-show或ng-style

来自分类Dev

UITableViewRowAction custom style?

来自分类Dev

Select element with certain style?

来自分类Dev

Defined component style not working

来自分类Dev

Change ftp css style

来自分类Dev

Setting global style in XAML

来自分类Dev

<style>标签内的Angularjs

来自分类Dev

Style the First Occurence of a Class

来自分类Dev

How to format text with style

来自分类Dev

与grb的style.background

来自分类Dev

对于<span style =

来自分类Dev

How to achieve this style of UITextField

来自分类Dev

覆盖 HTML Style="" 元素

来自分类Dev

Why is computed style in mobile safari differing from ruled style?

来自分类Dev

Style对象不能影响其应用的对象的Style属性

来自分类Dev

“ ol.style.Style数组”的优点是什么?

来自分类Dev

在angularJS中使用ng-style而不是style时出错

来自分类Dev

如何将类添加到<style> </ style>

来自分类Dev

父级中的XML @style

来自分类Dev

Angular.js <style>标签

来自分类Dev

Allow certain style attributes with ngSanitize

来自分类Dev

在TinyMCE中使用<style>标签