C# 如何为wpf文本框绑定fontsize?
我有一个文本框控件,它可以正常工作,无需绑定,而其字体大小应该动态更改 我想把它做好,使用绑定 我尝试使用xaml绑定FontSize:C# 如何为wpf文本框绑定fontsize?,c#,wpf,binding,textbox,font-size,C#,Wpf,Binding,Textbox,Font Size,我有一个文本框控件,它可以正常工作,无需绑定,而其字体大小应该动态更改 我想把它做好,使用绑定 我尝试使用xaml绑定FontSize: <UserControl x:Class="<ClassName>" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/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>
_multilientsize的唯一用途是替换我在事件等中使用的_textbox.Text
GetBestFittingFontSize是一个可以正常工作并计算我需要使用的字体大小的函数。视情况而定。它返回双倍
它不起作用。有人知道为什么吗?可能有一些DataContext问题?您可以尝试使用ViewBox来缩放其内容
<Viewbox StretchDirection="Both" Stretch="Uniform">
<local:UserControl1 Height="100" Width="100"/>
</Viewbox>
这也是谷歌给我的@帕特里克霍夫曼:-我明白了。我应该删除我的答案并投票以重复方式结束吗?@PatrickHofman:-我刚从OP中选择了这一行,而它的字体应该动态更改,这让我想到了使用ViewBox。你必须同意这个问题非常简单。@PatrickHofman:-确实如此。将等待几分钟,然后我将删除我的答案。我们不能说,因为您没有给我们提供XAML,我们甚至不知道控件类型,我们不知道返回的GetBestFittingFontSize是什么;是,如何设置多行大小,当前结果是什么,等等@PatrickHofman-感谢您的反馈。添加了很多细节,我想让它简短。。。可能太短了。。。很抱歉我投票决定重新开业。希望其他人也会效仿。
<Viewbox StretchDirection="Both" Stretch="Uniform">
<local:UserControl1 Height="100" Width="100"/>
</Viewbox>