[英]WPF - Binding FontFamily of TextBox to a ComboBox
我知道这篇文章: TextBox FontFamily Binding ,看起来类似于我的问题,但是我的情况要简单得多,我想将TextBox控件的FontFamily属性绑定到ComboBox,当ComboBox的值更改时,TextBox的内容会相应更改。 我没有使用VM或依赖项属性,我遵循了本教程: 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的值之后,什么也没发生。
谢谢你的时间。
return fontfamily;
的断点return fontfamily;
会表明您没有从value.ToString()
得到期望。 您需要从ComboBoxItem
获取Content
:
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
进行排序,所以您需要使用CollectionViewSource
进行排序 。
不必使用绑定转换器。
相反,您可以将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}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.