我有一个适当的工作,没有绑定,而其字体大小应动态更改的文本框控件。如何为wpf文本框绑定fontsize?
我想做的正确,并使用绑定工作。
我试图绑定(XAML)的字号使用:
<UserControl x:Class="<ClassName>"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Focusable="True"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox x:Name="_textBox" Visibility="Visible" xml:space="preserve"
Background="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=Background}"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=Foreground}"
FontFamily="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=FontFamily}"
BorderBrush="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=BorderBrush}"
Text="{Binding MultilineText}"
FontSize="{Binding Path=MultilineFontSize}"
KeyUp="_textBox_KeyUp"
PreviewTextInput="_textBox_PreviewTextInput"
DataObject.Pasting="_textBox_Pasting"
VerticalContentAlignment="Top"
PreviewKeyDown="TextBox_OnPreviewKeyDown"
TextWrapping="Wrap"
ScrollViewer.VerticalScrollBarVisibility="Visible"
/>
</Grid>
和在后面的代码:
private double _multilineFontSize;
public double MultilineFontSize
{
get { return GetBestFittingFontSize(); }
set
{
if (value != _multilineFontSize)
{
_multilineFontSize = value;
OnPropertyChanged("MultilineFontSize");
}
}
}
为_multilineFontSize的唯一用途是取代_textbox.Text无论我在哪里使用(在事件等)。
GetBestFittingFontSize()是一个函数(适用),并计算我需要使用的字体大小。按照给定的。它返回一倍。
它不起作用。有没有人知道为什么? (可能是一些DataContext问题?)
我们不能说因为你没有给我们的XAML(我们甚至不知道控制型),我们不知道是什么'返回GetBestFittingFontSize();'是,如何'_multilineFontSize '设置好,目前的结果是什么等等。 –
@PatrickHofman - 感谢您的反馈。增加了很多细节(我想把它缩短......这可能太短了......对不起!) – MTZ
我投票重新开放。希望别人会跟随。 –