简体   繁体   English

如何为WPF文本框绑定fontsize?

[英]how to bind fontsize for wpf textbox?

I have a textbox control that works appropriately, without binding, while its font size should be changed dynamically. 我有一个文本框控件,可以正常工作,没有绑定,而其字体大小应动态更改。

I wanted to do it right and work using binding. 我想正确地做,并使用绑定来工作。

I tried to bind the FontSize using (xaml): 我试图使用(xaml)绑定FontSize:

<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>

and in the code behind: 并在后面的代码中:

private double _multilineFontSize;
public double MultilineFontSize
    {
        get { return GetBestFittingFontSize();  }
        set
        {
            if (value != _multilineFontSize)
            {
                _multilineFontSize = value;
                OnPropertyChanged("MultilineFontSize");
            }
        }
    }

The only use for _multilineFontSize is to replace _textbox.Text wherever I used it (in events etc). _multilineFontSize的唯一用途是在我使用过的地方(在事件等地方)替换_textbox.Text。

GetBestFittingFontSize() is a function (that works appropriately) and calculates the font size I need to use. GetBestFittingFontSize()是一个函数(可以正常工作)并计算我需要使用的字体大小。 Take it as given. 按给定。 It returns double. 它返回两倍。

It doesn't work. 没用 Does any one have any idea why? 有谁知道为什么吗? (maybe some DataContext issues?) (也许是一些DataContext问题?)

You can try to use the ViewBox which can be used to scale its content. 您可以尝试使用可用于缩放其内容的ViewBox

<Viewbox StretchDirection="Both" Stretch="Uniform">
    <local:UserControl1 Height="100" Width="100"/>
</Viewbox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM