Vin*_*ANG 2 c# data-binding wpf
我知道这篇文章: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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,在我更改 ComboBox 的值后,没有任何反应。
谢谢你的时间。
断点 onreturn 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;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过将<FontFamily>对象直接添加到ComboBox:
<ComboBox DockPanel.Dock="Top" x:Name="Fonttype" SelectedIndex="0">
<FontFamily>Arial</FontFamily>
<FontFamily>Segoe UI</FontFamily>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
然后绑定到SelectedValue没有转换器。
FontFamily="{Binding Path=SelectedValue, ElementName=Fonttype, Mode=OneWay}"
Run Code Online (Sandbox Code Playgroud)
或者,您可能想自动获取已安装字体的列表:
<ComboBox DockPanel.Dock="Top" x:Name="Fonttype" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectedIndex="0" />
Run Code Online (Sandbox Code Playgroud)
尽管不太可能对SystemFontFamilies进行排序,因此您需要使用 aCollectionViewSource对 进行排序。
| 归档时间: |
|
| 查看次数: |
2427 次 |
| 最近记录: |