WPF - 将TextBox的FontFamily绑定到ComboBox

时间:2016-04-20 04:23:48

标签: c# wpf data-binding

我知道这篇文章:TextBox FontFamily Binding,看起来与我的问题相似,但我的情况要简单得多,我想将TextBox控件的FontFamily属性绑定到ComboBox,当ComboBox的值更改后,TextBox的内容会相应更改。我没有使用VM或Dependency Property,我遵循了本教程:https://dotnetstories.wordpress.com/2011/07/31/using-type-converters-in-wpf/

并提出:

<Window x:Class="NumericControlTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:NumericControlTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <local:FontFamilyConversions x:Key="FontFamilyConversions" />
</Window.Resources>

<DockPanel>

    <ComboBox  DockPanel.Dock="Top" x:Name="Fonttype" >
        <ComboBoxItem IsSelected="True">Arial</ComboBoxItem>
        <ComboBoxItem>Batang</ComboBoxItem>
        <ComboBoxItem>BatangChe</ComboBoxItem>
        <ComboBoxItem>Gungsuh</ComboBoxItem>
        <ComboBoxItem>GungsuhChe</ComboBoxItem>
        <ComboBoxItem>Courier New</ComboBoxItem>
    </ComboBox>
    <TextBox x:Name="editor" FontSize="16" FontFamily="{Binding Path=SelectedValue, ElementName=Fonttype, Mode=OneWay, Converter={StaticResource FontFamilyConversions}}" >

    </TextBox>
</DockPanel>

using System.Windows.Data;
using System.Windows.Media;

namespace NumericControlTest
{
    class FontFamilyConversions : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            FontFamily fontfamily = new FontFamily("Verdana");
            if (value != null)
            {
                fontfamily = new FontFamily(value.ToString());
            }
            return fontfamily;
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}

但它不起作用,在我更改ComboBox的值后,没有任何反应。

感谢您的时间。

2 个答案:

答案 0 :(得分:3)

return fontfamily;上的断点表明您没有得到value.ToString()的期望。您需要从Content

获取ComboBoxItem
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    FontFamily fontfamily = new FontFamily("Verdana");
    ComboBoxItem selectedFont = value as ComboBoxItem;
    if (selectedFont != null)
    {
        fontfamily = new FontFamily(selectedFont.Content.ToString());
    }
    return fontfamily;
}

您可以通过将<FontFamily>对象直接添加到ComboBox来避免转换器:

<ComboBox  DockPanel.Dock="Top" x:Name="Fonttype" SelectedIndex="0">
    <FontFamily>Arial</FontFamily>
    <FontFamily>Segoe UI</FontFamily>            
</ComboBox>

然后在没有转换器的情况下绑定到SelectedValue

FontFamily="{Binding Path=SelectedValue, ElementName=Fonttype, Mode=OneWay}"

或者您可能想自动获取已安装字体的列表:

<ComboBox  DockPanel.Dock="Top" x:Name="Fonttype" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectedIndex="0" />

虽然SystemFontFamilies不太可能被排序,但您需要use a CollectionViewSource to sort

答案 1 :(得分:1)

没有必要使用绑定转换器。

相反,您可以将FontFamily分配给每个ComboBoxItem的Tag属性。这也可以让您为ComboBox项使用任意文本:

<ComboBox x:Name="fontFamilyComboBox" SelectedValuePath="Tag">
    <ComboBoxItem Content="Arial">
        <ComboBoxItem.Tag>
            <FontFamily>Arial</FontFamily>
        </ComboBoxItem.Tag>
    </ComboBoxItem>
    <ComboBoxItem Content="Courier">
        <ComboBoxItem.Tag>
            <FontFamily>Courier New</FontFamily>
        </ComboBoxItem.Tag>
    </ComboBoxItem>
</ComboBox>

<TextBox Text="Hello, World."
         FontFamily="{Binding SelectedValue, ElementName=fontFamilyComboBox}"/>

FontFamily属性上的SelectedValue绑定直接返回ComboBox的Tag属性,因为在ComboBox上设置了SelectedValuePath="Tag"。没有它,FontFamily绑定也可以这样写:

<TextBox Text="Hello, World."
         FontFamily="{Binding SelectedItem.Tag, ElementName=fontFamilyComboBox}"/>