WPF-将文本框的FontFamily绑定到组合框
本文关键字:绑定 组合 FontFamily 文本 WPF- | 更新日期: 2024-10-19 19:23:27
我知道这篇文章: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;
上的断点将表明您没有从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>
然后在没有转换器的情况下绑定到CCD_ 7。
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 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
属性,因为SelectedValuePath="Tag"
是在ComboBox上设置的。如果没有这些,FontFamily绑定也可以这样写:
<TextBox Text="Hello, World."
FontFamily="{Binding SelectedItem.Tag, ElementName=fontFamilyComboBox}"/>