【发布时间】:2016-04-20 04:23:47
【问题描述】:
我知道这个帖子:TextBox FontFamily Binding,看起来类似于我的问题,但我的情况要简单得多,我想在 ComboBox 的值更改时将 TextBox 控件的 FontFamily 属性绑定到 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 的值后,没有任何反应。
感谢您的宝贵时间。
【问题讨论】:
标签: c# wpf data-binding